search
HomeBackend DevelopmentPHP Tutorialmysql数据库类中出现的问题

mysql

 这是我的mysql类,继承至db抽象类,其中db的抽象方法在mysql中都以实现,
db类:
abstract class db{
//连接数据库
abstract public function connect($h,$u,$p);
//发送查询
abstract public function query($sql);
//查询多行数据
abstract public function getAll($sql);
//查询单行数据
abstract public function getOne($sql);

//查询单个数据
abstract public function getRow($sql);
abstract public function autoExecute($arr,$table,$mode='insert ',$where='1 limit 1');
}
class mysql extends db{
private static $ins=null;
private $conn=null;
private $conf=array();
//读取数据库的配置信息
protected function __construct(){
$this->conf=conf::getIns();
$this->select_db($this->conf->db);
$this->connect($this->conf->host,$this->conf->user,$this->conf->pwd);//显示是空
$this->setChar($this->conf->char);
}
public function __destruct(){

}
//使用单例模式,只允许new一次
public static function getIns(){
if(self::$ins===null){
self::$ins=new self();
}
return self::$ins;
}
//连接,连接失败时,抛出异常
public function connect($h,$u,$p){
//没走到这一步
$this->conn=mysql_connect($h,$u,$p);
}
protected function select_db($db){
$sql='use '.$db;
$this->query($sql);
}

protected function setChar($char){
$sql='set names '.$char;
return $this->query($sql);
}
//发送数据
public function query($sql){
/*if($this->conf->debug){
log::write($sql);
}*/
/* var_dump($sql);
exit;*/
$rs=mysql_query($sql,$this->conn);
/*if(!rs){
log::write($this->error());
}*/
if(!$rs){
echo '失败
';
var_dump($this->conn);
echo '
';
var_dump($this->conf);


}
return $rs;
}
//自动进行计算
 public function autoExecute($arr,$table,$mode='insert ',$where='1 limit 1'){
  if(!is_array($arr)){
  return false;
  }//更新表中的数据
  if($mode=='update'){
  $sql='update '.$table.'set';
  foreach($arr as $k=>$v){
  $sql.=$k."='".$v."',";
  }
  $sql=rtrim($sql,',');
  $sql.=$where;
  return $this->query($sql);
  }
  $sql='insert into '.$table.'('.implode(',',array_keys($arr)).')';
  $sql.='values(\'';
  $sql.=implode("','",array_values($arr));
  $sql.='\')';
  return $this->query($sql);
 }
 //取出表中所有符合条件的多行数据
 public function getAll($sql){
  $rs=$this->query($sql);
  $list=array();
  while($row=mysql_fetch_assoc($rs)){
  $list[]=$row;
  }
  return $list;
 }
 //取出符合条件的一行数据
 public function getRow($sql){
  $rs=$this->query($sql);
  return mysql_fetch_assoc($rs);
 }
 //取出一个数据
 public function getOne($sql){
  $rs=$this->query($sql);
  $row=mysql_fetch_assoc($rs);
  return $row[0];
 }
 //取出影响的数据
 public function affected_rows(){
  return mysql_affected_rows($this->conn);
 }
 //插入一个id
public function insert_id(){
return mysql_insert_id($this->conn);
}
}

为什么会出现:Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource
排查了很多错误,发现程程序没有走到
public function connect($h,$u,$p){
//没走到这一步
$this->conn=mysql_connect($h,$u,$p);
}
想不出什么原因

回复讨论(解决方案)

//读取数据库的配置信息
protected function __construct(){

你把构造函数指定成 保护模式
如何能被执行?

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools