$db = new mysqli('localhost', 'root', '', 'tran');
$name = 'jack';$sql_1 = sprintf('INSERT INTO `tb_user` (name)VALUES("%s")',mysql_real_escape_string($name));$db->query($sql_1);为什么会出现Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in这两个句错误的,我知道如果连不上数据库mysql_real_escape_string是会报错的,但我明明连接上了,
如果换回mysql_connect() mysql_select_db mysql_query()这样的链接就没问题还有就是我的字段 name 设置的是not null 为什么上面的sql语句执行了抛出 Warning 这样严重的错误还会保存一个空值的?not null 不是不能为空否则不能保存的吗?

解决方案 »

  1.   

    mysqli和mysql模块不是一回事。mysql_real_escape_string()需要 mysql_connect()建立的连接。
    想实现同样的功能,参见: mysqli_real_escape_string 这个是mysqli模块下的函数。
    不过大多数用PDO或者mysqli的查询都会采用类似的方法:
    http://www.php.net/manual/zh/mysqli.prepare.php
    数据由模块根据类型设置自动转义,像你的C风格写法很少见。
      

  2.   

    class db_mysqli extends mysqli
     {
     
      private $link;
      private $result;
      private $bol=false;
     
       function __construct(){
      $this->open_link();
      }
     
      //开启连接
      private function open_link(){
      $this->link=new mysqli($GLOBALS['HOST'],$GLOBALS['USER'],$GLOBALS['PASSWORD'],$GLOBALS['DBNAME']) or die("数据库连接失败!".mysqli_error());
      $this->link->select_db($GLOBALS['DBNAME']) or die("数据库选择失败!".mysqli_error());
      $this->link->set_charset("UTF-8");
      }
     
      //关闭连接
      private function close_result(){
      //if($this->result){
      //$this->result->free_sult();//关闭结果集
      //}
     
      }
     
     
     
      public function db_query($sqlstr){
      if(empty($this->link)){
      $this->open_link();
         }
      $this->result=$this->link->query($sqlstr);
      if($this->result){
      $row=$this->result->fetch_array();
      }else {
      $row=null;
      }
      $this->close_result();
      return $row;
      }
    我一般是这样写的 ,你写的我看了好像也没什么问题 ,你看一下 ,希望可以帮助你。