我正在为我的cms切换到mysqli,我在切换查询时出现了一些问题。获取mysqli的函数,我不断收到错误说
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u250000297/public_html/forum/system/db.php on line 48 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 49 Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u250000297/public_html/forum/system/db.php on line 48 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 49 Warning: mysqli_real_query() expects parameter 1 to be mysqli, null given in /home/u250000297/public_html/forum/system/db.php on line 41 Warning: mysqli_error() expects parameter 1 to be mysqli, null given in /home/u250000297/public_html/forum/system/db.php on line 41
我尝试了不同的quires:
function query($query) {
$sql = mysqli_real_query($connection, $query) or die (mysqli_error($connection));
return $sql;
mysqli_free_result($sql);
}
function fetch($query) {
$data = mysqli_query($connection, $query);
mysqli_fetch_array($data);
return($data);
}
2:
function query($query) {
$sql = mysqli_query($this->db, $query) or die(mysqli_error());
return $sql;
mysqli_free_result($sql);
}
function fetch($query) {
$data = mysqli_query($this->db, $query);
mysqli_fetch_array($data);
return($data);
}
我一直在犯同样的错误,我不知道我做错了什么或者做错了什么。在尝试使用mysqli
之前,你能帮助我并给我一些提示吗?我的原始代码function query($query) {
$sql = mysql_query($query, $this->db) or die(mysql_error());
return $sql;
mysql_free_result($sql);
}
function fetch($query) {
$sql = mysql_fetch_array(mysql_query($query, $this->db));
return $sql;
mysql_free_result($sql);
}
答案 0 :(得分:0)
首先建立连接和数据库名称
$this->con = mysqli_connect($this->host,$this->user,$this->pass);
mysqli_select_db( $this->con,$this->db );
然后像这样执行查询
function query_execute( $query )
{
$this->query = mysqli_query( $this->con,$query );
if ($this->query->num_rows > 0 ) {
return $this->query;
} else {
return mysqli_error( $this->con );
}
}