2014-07-26 161 views
11

我有一個問題,我無法從我的MySQL數據庫(通過PHP)檢索結果。我在其他地方使用相同的功能,並且工作完美無瑕。然而,在這一點上,我不斷收到「警告:mysqli_query():無法獲取mysqli」錯誤。下面解釋了這個問題的細節。 我用一個非常類似的功能在別處(getAllCountries如下圖所示),在我的PHP這也很好地工作:警告:mysqli_query():無法獲取mysqli

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 
+1

這不是完整的信息。就像你檢查連接錯誤一樣,你也應該檢查查詢錯誤。我對mysqli不熟悉,但是如果你打開這個手冊,你會發現它的名字上有'error'錯誤。 –

+0

爲什麼在'getUserPicPath()'中運行查詢兩次?第一次通話後的「回聲」會給你什麼? – andrewsi

+1

我還建議檢查一下'mysqli_error()'裏面的內容,這樣你就可以看到數據庫傳回的錯誤 – andrewsi

回答

12

我認爲這是因爲當你關閉數據庫連接的第一次,你忘記了:

unset($connection); 

然後當你試圖連接到數據庫再次,它胡扯,因爲它仍設置爲關閉的連接。

-2

您忘記了包含您的數據庫連接。只需將$connection添加到您的sql查詢中:

function getAllCountries() 
{ 
    $result = db_query($connection,"SELECT countryid, name FROM country ORDER BY name ASC"); 

    // enter code here 
} 
相關問題