我无法使用 PHP 从 MySQL 数据库中获取记录。这是我的代码。
用户.php:
require_once ('../../include/dbconfig.php');
require_once ('common.php');
error_reporting(E_ALL);
ini_set('display_errors', '1');
$userClass=new CommonConnectorFuncs();
$redata=$userClass->insertUserRecordForSignup();
echo $redata;exit;
common.php:
require_once ('../../include/dbconfig.php');
error_reporting(E_ALL);
ini_set('display_errors', '1');
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") ? "https" : "http";
$imagepath=$protocol. "://" . $_SERVER['HTTP_HOST']."/connector/upload/";
class CommonConnectorFuncs{
function __construct() {
}
// destructor
function __destruct() {
// $this->close();
}
public function insertUserRecordForSignup(){
$sql=mysqli_query($connect,"select * from cn_user_info order by user_id desc");
while ($row=mysqli_fetch_array($sql)) {
$data[]=$row;
}
return $data;
}
}
在这里,我试图通过类获取记录和打印,但它抛出以下消息。
Notice: Undefined variable: connect in common.php on line 16
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in common.php on line 16
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in common.php on line 17
Notice: Undefined variable: data in common.php on line 20
这些查询在 user.php
文件中运行良好,但在 common.php
文件中不起作用。
最佳答案
如评论中所述,这是一个范围界定问题。具体来说,$connect 不在范围内。
Notice: Undefined variable: connect in common.php on line 16 where connect is not defined anywhere.
此外,这与您错误地将参数传递给 mysqli_query 时的错误状态完全一样。假设 $connect 是由 new mysqli()
在某个时刻生成的 mysqli 连接,它应该是:
$sql = "select * from cn_user_info order by user_id desc";
$result = mysqli_query( $connect,$sql) or die('Could not look up user information; ' . mysqli_error($connect))
希望对您有所帮助!
关于php - 无法使用 PHP 和 MySQL 获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43585645/