我遇到一个问题,我无法从MySQL数据库中检索结果(通过PHP)。我在其他地方使用相同的功能,它完美无瑕。但是在这一点上我继续得到"警告:mysqli_query():无法获取mysqli"错误。下面解释问题的细节。 我在我的PHP中使用了一个非常相似的函数(如下所示的getAllCountries),它可以完美地工作:
function getAllCountries()
{
$result = db_query("SELECT countryid, name FROM country ORDER BY name ASC");
echo "<select class=addresscountry name=country>";
while($row = mysqli_fetch_array($result)) {
echo '<option value="' . $row['countryid'] . '">' . $row['name'] . '</option>';
}
echo "</select>";
mysqli_close(db_connect());
}
所以问题如下:
我有一个包含以下代码的php文件:
<?php
require 'includes/functions.php';
function getUserPicPath()
{
$userid = $_SESSION['userid'];
$result = db_query("SELECT picture FROM user WHERE userid='$userid'");
while($row = mysqli_fetch_array($result)) {
$picturepath = $row['picture'];
}
echo $picturepath;
mysqli_close(db_connect());
}
我的functions.php文件包含以下行(与其他不相关的函数一起):
require 'dbfunctions.php';
和我的dbfunctions.php看起来像这样:
<?php
function db_connect()
{
require ".db_password.php";
static $connection;
if(!isset($connection)) {
$connection = mysqli_connect('localhost',$username,$password,$dbname);
}
if($connection === false) {
return mysqli_connect_error();
}
return $connection;
}
function db_query($query)
{
$connection = db_connect();
$result = mysqli_query($connection,$query);
return $result;
}
在我的PHP文档中,我调用以下函数:
if ($userid == -1)
{
showNotAuthorizedPage();
} else {
myAccountPage();
}
和myAccountPage()函数在与getUserPicPath()函数相同的文件中声明,此getUserPicPath()函数调用如下:
<div id="tabs-2">
<p><?php getUserPicPath(); ?></p>
</div>
我在我的网页上使用标签(http://jqueryui.com/tabs/#default),这就是我想要调用它的地方。 myAccountPage()函数,它给出以下错误:
Warning: mysqli_query(): Couldn't fetch mysqli in C:\Users\Dennis\Documents\My Dropbox\xxx\zzz\www\Project Files\includes\dbfunctions.php on line 29
Call Stack
# Time Memory Function Location
1 0.0000 256880 {main}( ) ..\myaccount.php:0
2 0.0010 283328 myAccountPage( ) ..\myaccount.php:181
3 0.0070 285368 getUserPicPath( ) ..\myaccount.php:121
4 0.0070 285528 db_query( ) ..\myaccount.php:11
5 0.0070 285624 mysqli_query ( ) ..\dbfunctions.php:29
( ! ) Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\Users\Dennis\Documents\My Dropbox\me&roxy\WE\final project\Project Files\myaccount.php on line 13
Call Stack
# Time Memory Function Location
1 0.0000 256880 {main}( ) ..\myaccount.php:0
2 0.0010 283328 myAccountPage( ) ..\myaccount.php:181
3 0.0070 285368 getUserPicPath( ) ..\myaccount.php:121
4 0.0080 285768 mysqli_fetch_array ( ) ..\myaccount.php:13
( ! ) Notice: Undefined variable: picturepath in C:\Users\Dennis\Documents\My Dropbox\me&roxy\WE\final project\Project Files\myaccount.php on line 17
Call Stack
# Time Memory Function Location
1 0.0000 256880 {main}( ) ..\myaccount.php:0
2 0.0010 283328 myAccountPage( ) ..\myaccount.php:181
3 0.0070 285368 getUserPicPath( ) ..\myaccount.php:121
( ! ) Warning: mysqli_close(): Couldn't fetch mysqli in C:\Users\Dennis\Documents\My Dropbox\me&roxy\WE\final project\Project Files\myaccount.php on line 19
Call Stack
# Time Memory Function Location
1 0.0000 256880 {main}( ) ..\myaccount.php:0
2 0.0010 283328 myAccountPage( ) ..\myaccount.php:181
3 0.0070 285368 getUserPicPath( ) ..\myaccount.php:121
4 0.0100 285864 mysqli_close ( ) ..\myaccount.php:19
答案 0 :(得分:12)
我认为这是因为当你第一次关闭数据库连接时,你忘记了:
unset($connection);
然后当你再次尝试连接数据库时,它会崩溃,因为它仍然设置为已关闭的连接。
答案 1 :(得分:-3)
您忘记了包含数据库连接。只需将$connection
添加到您的SQL查询:
function getAllCountries()
{
$result = db_query($connection,"SELECT countryid, name FROM country ORDER BY name ASC");
// enter code here
}