我们很难用这段代码将mysql_query转换为mysqli,错误是
警告:mysqli_query()期望参数1为mysqli,null给定 在C:\ xampp \ htdocs \ Projects \ MeterReading \ include \ functions.php上 45
这是错误行
上的function.php的代码//Display Data
function DisplayBLLData($db) {
//global $conn;
$sql = "SELECT * from `bl_inv_discon`";
$result = $db->query($sql);
var_dump($result); die();
return $result;
}
这是另一个用于查询的代码
<?php
include_once 'config.php';
class DB extends mysqli {
function DB() {
global $dbhost, $dbuser, $dbpwd, $dbname;
$conn = mysqli_connect($dbhost, $dbuser, $dbpwd, $dbname) or die("Failed to connect to server!" . mysqli_error());
return $conn;
}
/*function selectDB($dbname) {
global $dbhost, $dbuser, $dbpwd;
$conn = mysqli_connect($dbhost, $dbuser, $dbpwd) or die("Failed to connect to server!" . mysqli_error());
$result = mysqli_select_db($dbname, $conn) or die("Failed to select database!" . mysqli_error());
return $result;
}*/
function query($sql) {
global $dbhost, $dbuser, $dbpwd, $dbname;
// global $conn;
$conn = mysqli_connect($dbhost, $dbuser, $dbpwd, $dbname) or die("Failed to connect to server!" . mysqli_error());
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
//var_dump($conn); die();
return $result;
}
function num_rows($data) {
return mysqli_num_rows($data);
}
function fetch_array($result) {
return mysqli_fetch_array($result);
}
function fetch_assoc($data) {
return mysqli_fetch_assoc($data);
}
function escape_string($data) {
return mysqli_real_escape_string($data);
}
}
$db = new DB;
?>
答案 0 :(得分:0)
您在此处所做的只是创建类DB的对象而不是为连接调用函数DB。我假设您正在考虑DB()作为构造函数,但在PHP构造函数中以不同的方式创建(使用__construct
关键字)。你需要做的只是简单。
$obj=new DB();
$db=$obj->DB();
然后将$db
作为参数传递给您的函数,我希望它能正常工作。