我有一个问题,我无法从我的MySQL数据库中检索结果(通过PHP)。我在其他地方使用相同的函数,它可以完美地工作。然而,在这一点上,我一直收到"Warning: mysqli_query():the‘t fetch 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();
}<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发布于 2014-08-13 01:00:43
我认为这是因为当您第一次关闭数据库连接时,您忘记了:
unset($connection);然后,当您再次尝试连接到数据库时,它会失败,因为它仍然设置为关闭的连接。
发布于 2017-08-22 19:12:16
您忘记了包含您的数据库连接。只需将$connection添加到您的sql查询中:
function getAllCountries()
{
$result = db_query($connection,"SELECT countryid, name FROM country ORDER BY name ASC");
// enter code here
}https://stackoverflow.com/questions/24973330
复制相似问题