php - 无法使用 PHP 和 MySQL 获取记录

标签 php mysql

我无法使用 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/

相关文章:

php - 管理面板和用户个人资料同时登录

php - 为什么 PHP 在向以 proc_open 启动的进程写入 4096 字节后会挂起?

php - 使用 php 创建和写入文件

android - 单个textview android中的多行数据

php - mb_detect_encoding 将 ASCII 检测为 UTF-8?

mysql - 按字段中的子字符串分组

mysql - mysql 有没有返回全部的变量?

php - Laravel 数据库模式,可以为空的外部

php 表单没有提交到 MySQL 数据库

php - 如何在 PHP 中将数组分组并编码为 JSON?