在非对象上调用成员函数 query()
Call to a member function query() on a non-object in
我正在尝试 php 来简化我的数据库连接,以便我可以修改代码的一个区域并影响整个程序。这是我拥有的:
页面:rdpa.php(模块页面
/**************************/
/* Create System Settings */
/**************************/
/* Database Connection Settings */
$_SESSION['servername'] = "localhost";
$_SESSION['mysql_username'] = "username";
$_SESSION['mysql_password'] = "password";
$_SESSION['dbname'] = "mydb";
//Turn on Error Report. True = On / False = Off
//ErrorReporting(false);
//Display Error.kfkg
function ErrorReporting($ErrOn){
if ($ErrOn == true) {
//Show Error
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
}
}
/**************************************
Open Database Connection Function.
***************************************/
class db_class {
function db_conn() {
global $conn;
global $mysqli;
$conn = new mysqli($_SESSION['servername'], $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
//return db_conn;
}
}
页数:testdb3.php
<?php
//Included file.
include 'modules/rdpa.php';
error_reporting(-1);
ini_set('display_errors', 'On');
//Access our class.
$db_Class = new db_Class;
$conn = ($db_Class->db_conn());
//connect to the database.
$sql = "SELECT id, region FROM tbl_region;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<td width="1"><label for="delete">
<input type="radio" name="region" id="region" value="'.$row["id"].'">
</label></td>';
echo '<td align="left" valign="top"><p>'.$row["region"].'</p></td></tr>';
}
}
?>
当我 运行 testdb3.php 我得到以下错误:
致命错误:在 C:\websites\rdpa\testdb3.php 中的非对象上调用成员函数 query() 第 15 行
这是第 15 行:
//connect to the database.
$result = $conn->query($sql);
有人能告诉我我做错了什么吗?
我认为您在查询结束后遇到分号问题。
$sql = "SELECT id, region FROM tbl_region;"; your current query please replace with below query which i have given below. may it helps you.
$sql = "SELECT id, region FROM tbl_region";
试试上面的查询,它会解决你的问题。并替换您没有 return conn 对象的函数代码。替换为以下代码。你的问题就解决了。
function db_conn() {
global $conn;
global $mysqli;
$conn = new mysqli($_SESSION['servername'], $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
}
return $conn;
}
将您的 rdpa.php 页面更改为
class db_class {
function db_conn() {
global $conn;
global $mysqli;
$conn = new mysqli($_SESSION['servername'], $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Test if connection succeeded
if (mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
}
return $conn; //return connection string
}
} //this is missing in your code
您需要 return 来自函数的连接字符串。现在你在 if
循环中 returning $conn
即如果发生错误然后 return 连接这就是你正在做的。
请使用一些IDE来编码。 }
for class
在您的代码中缺失
我正在尝试 php 来简化我的数据库连接,以便我可以修改代码的一个区域并影响整个程序。这是我拥有的:
页面:rdpa.php(模块页面
/**************************/
/* Create System Settings */
/**************************/
/* Database Connection Settings */
$_SESSION['servername'] = "localhost";
$_SESSION['mysql_username'] = "username";
$_SESSION['mysql_password'] = "password";
$_SESSION['dbname'] = "mydb";
//Turn on Error Report. True = On / False = Off
//ErrorReporting(false);
//Display Error.kfkg
function ErrorReporting($ErrOn){
if ($ErrOn == true) {
//Show Error
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
}
}
/**************************************
Open Database Connection Function.
***************************************/
class db_class {
function db_conn() {
global $conn;
global $mysqli;
$conn = new mysqli($_SESSION['servername'], $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
//return db_conn;
}
}
页数:testdb3.php
<?php
//Included file.
include 'modules/rdpa.php';
error_reporting(-1);
ini_set('display_errors', 'On');
//Access our class.
$db_Class = new db_Class;
$conn = ($db_Class->db_conn());
//connect to the database.
$sql = "SELECT id, region FROM tbl_region;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<td width="1"><label for="delete">
<input type="radio" name="region" id="region" value="'.$row["id"].'">
</label></td>';
echo '<td align="left" valign="top"><p>'.$row["region"].'</p></td></tr>';
}
}
?>
当我 运行 testdb3.php 我得到以下错误:
致命错误:在 C:\websites\rdpa\testdb3.php 中的非对象上调用成员函数 query() 第 15 行
这是第 15 行:
//connect to the database.
$result = $conn->query($sql);
有人能告诉我我做错了什么吗?
我认为您在查询结束后遇到分号问题。
$sql = "SELECT id, region FROM tbl_region;"; your current query please replace with below query which i have given below. may it helps you.
$sql = "SELECT id, region FROM tbl_region";
试试上面的查询,它会解决你的问题。并替换您没有 return conn 对象的函数代码。替换为以下代码。你的问题就解决了。
function db_conn() {
global $conn;
global $mysqli;
$conn = new mysqli($_SESSION['servername'], $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
}
return $conn;
}
将您的 rdpa.php 页面更改为
class db_class {
function db_conn() {
global $conn;
global $mysqli;
$conn = new mysqli($_SESSION['servername'], $_SESSION['mysql_username'], $_SESSION['mysql_password'], $_SESSION['dbname']);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Test if connection succeeded
if (mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
}
return $conn; //return connection string
}
} //this is missing in your code
您需要 return 来自函数的连接字符串。现在你在 if
循环中 returning $conn
即如果发生错误然后 return 连接这就是你正在做的。
请使用一些IDE来编码。 }
for class
在您的代码中缺失