du8864 2017-04-24 10:26
浏览 16

无法使用PHP和MySQL获取记录

I have an issue. I am unable to fetch record from mysql database using PHP. I am explaining my code below.

user.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;
    }
}

Here I am trying to fetch record and print through class but its throwing the below message.

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

Those query is working fine inside the user.php file but its not working in common.php file.Please help me to resolve this issue.

  • 写回答

3条回答 默认 最新

  • doubi2145 2017-04-24 10:32
    关注

    As mentioned in comments, this is a scoping issue. Specifically, $connect is not in scope.

    Notice: Undefined variable: connect in common.php on line 16 where connect is not defined anywhere.

    Moreover, It's exactly as the error states as you're passing arguments to mysqli_query incorrectly. Assumming $connect is your mysqli connection generated at some point by new mysqli() it should be:

    $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))
    

    Hope it helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line