首页 / MYSQL / 为什么newmysqli会出错?
为什么newmysqli会出错?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了为什么newmysqli会出错?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3391字,纯文字阅读大概需要5分钟。
内容图文
![为什么newmysqli会出错?](/upload/InfoBanner/zyjiaocheng/197/97360c81aa054f91bab21d559f2a9424.jpg)
$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 不是不能为空否则不能保存的吗?
回复讨论(解决方案)
mysqli和mysql模块不是一回事。mysql_real_escape_string()需要 mysql_connect()建立的连接。
想实现同样的功能,参见: mysqli_real_escape_string 这个是mysqli模块下的函数。
不过大多数用PDO或者mysqli的查询都会采用类似的方法:
http://www.php.com/manual/zh/mysqli.prepare.php
数据由模块根据类型设置自动转义,像你的C风格写法很少见。
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; }
我一般是这样写的 ,你写的我看了好像也没什么问题 ,你看一下 ,希望可以帮助你。。。。
mysqli和mysql模块不是一回事。mysql_real_escape_string()需要 mysql_connect()建立的连接。
想实现同样的功能,参见: mysqli_real_escape_string 这个是mysqli模块下的函数。
不过大多数用PDO或者mysqli的查询都会采用类似的方法:
http://www.php.com/manual/zh/mysqli.prepare.php
数据由模块根据类型设置自动转义,像你的C风格写法很少见。
对我很有启发
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; }
我一般是这样写的 ,你写的我看了好像也没什么问题 ,你看一下 ,希望可以帮助你。。。。
对我很有启发
内容总结
以上是互联网集市为您收集整理的为什么newmysqli会出错?全部内容,希望文章能够帮你解决为什么newmysqli会出错?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。