简单代码求解答,小弟实在不懂为什么返回失败!该如何处理

WBOY
Release: 2016-06-13 13:35:41
Original
823 people have browsed it

简单代码求解答,小弟实在不懂为什么返回失败!
我想往数据库里添加数据,可以失败了,这是我的sql类,再往下的代码是我调用这个类的代码,希望好心人帮解答下,感激不尽!
  class SqlTool{
  private $conn;
private $host="localhost";
private $user="root";
private $password="root";
private $db="test";

function SqlTool(){
$this->conn=mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn){
die("connect faild!".mysql_error());
}
mysql_select_db($this->db,$this->conn);
mysql_query("set names gbk");
  }
  //完成select
  //function execute_dql($sql){
  
  //}
  //function:update ,delete,insert
  function execute_dml($sql){
  $b=mysql_query($sql);
//var_dump($b);
if(!$b){
return 0;//faild
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//success
}else{
return 2;//no influence
}
}
  }
}
?>





  require_once "mysqlTool.php";
  //create mysql
  $sql="insert into user1(name,password,email,age) values ('小明',md5(123),'xiaoming@sina.com',20)";
  $sqlTool=new SqlTool();
  $res=$sqlTool->execute_dml($sql);
  //var_dump($res);
  if($res==0){
  echo "faild!";
  }else if($res==1){
  echo "success";
  }else if($res==2){
  echo "no influence";
  }
?>







------解决方案--------------------
失败了肯定是sql指令执行失败了。

$b=mysql_query($sql) or die(mysql_error());
这样,如果有错误就会提示了。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template