您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

为啥 new mysqli 会出错

2024/4/7 18:08:19发布10次查看
为什么 new mysqli 会出错?
$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 不是不能为空否则不能保存的吗?
分享到:
------解决方案--------------------
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风格写法很少见。
------解决方案--------------------
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;
}
我一般是这样写的 ,你写的我看了好像也没什么问题 ,你看一下 ,希望可以帮助你。。。。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product