[英]Can not fetch record using PHP and MySQL
I have an issue. 我有一个问题。 I am unable to fetch record from mysql database using PHP. 我无法使用PHP从mysql数据库中获取记录。 I am explaining my code below. 我在下面解释我的代码。
user.php: 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: 的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. 这些查询在user.php
文件中工作正常,但它不能在common.php
文件中工作。请帮我解决这个问题。
As mentioned in comments, this is a scoping issue. 正如评论中所提到的,这是一个范围问题。 Specifically, $connect is not in scope. 具体来说,$ connect不在范围内。
Notice: Undefined variable: connect in common.php on line 16 where connect is not defined anywhere. 注意:未定义的变量:在第16行的common.php中连接, 其中connect未在任何地方定义。
Moreover, It's exactly as the error states as you're passing arguments to mysqli_query incorrectly. 而且,正如你错误地将参数传递给mysqli_query一样,它就像错误一样。 Assumming $connect is your mysqli connection generated at some point by new mysqli()
it should be: 假设$ connect是你的mysqli连接在某些时候由new 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))
Hope it helps! 希望能帮助到你!
The problem is in that you are trying to access a global
variable within a function
. 问题在于您正在尝试访问function
的global
变量。
First of all, make sure you include
the php file with the relevant database connection. 首先,确保include
带有相关数据库连接的php文件。 And as you are trying to access a global variable there are two ways to achieve this. 当您尝试访问全局变量时,有两种方法可以实现此目的。
Method 1 方法1
Create a global
variable at the top of the function. 在函数顶部创建一个global
变量。
global $connect;
But as Qirel says in this comment , it is a bad practise, so I'd suggest the next. 但正如Qirel在这篇评论中所说,这是一个不好的做法,所以我建议下一个。
Method 2 方法2
Pass the connection to the function's parameters. 将连接传递给函数的参数。
public function insertUserRecordForSignup($connect){
$sql=mysqli_query($connect,"select * from cn_user_info order by user_id desc");
while ($row=mysqli_fetch_array($sql)) {
$data[]=$row;
}
return $data;
}
Hope you find this useful. 希望您觉得这个有帮助。
Make the connect variable a property of your class as by parsing it in your construct function 通过在构造函数中解析它,使connect变量成为类的属性
require_once ('../../include/dbconfig.php');
require_once ('common.php');
error_reporting(E_ALL);
ini_set('display_errors', '1');
$userClass=new CommonConnectorFuncs($connect);
$redata=$userClass->insertUserRecordForSignup();
echo $redata;exit;
In your class, 在你的班上,
class CommonConnectorFuncs{
var $Connection;
function __construct($connection) {
try{
if($connection != null){
$this->Connection = $connection;
}else{
throw new Exception('Connection is null. COuld not process further');
}
}catch(Exception $ex){
echo $ex->getMessage();
exit;
}
}
// destructor
function __destruct() {
// $this->close();
}
public function insertUserRecordForSignup(){
$sql=mysqli_query($this->Connection,"select * from cn_user_info order by user_id desc");
while ($row=mysqli_fetch_array($sql)) {
$data[]=$row;
}
return $data;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.