2014-07-26 18 views
11

Ho un problema in cui non riesco a recuperare il risultato dal mio database MySQL (tramite PHP). Uso la stessa funzione in altri posti e funziona perfettamente. Comunque a questo punto continuo a ricevere l'errore "Warning: mysqli_query(): Could not get mysqli". I dettagli del problema sono spiegati di seguito. Io uso una funzione molto simile altrove (getAllCountries come mostrato qui sotto) nel mio PHP che funziona perfettamente:Avviso: mysqli_query(): Impossibile recuperare 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()); 
} 

Quindi il problema è il seguente:

Ho un file php che contiene il seguente codice:

<?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()); 
} 

il mio file functions.php ha la seguente riga (insieme ad altre funzioni non rilevanti):

require 'dbfunctions.php'; 

e la mia dbfunctions.php si presenta così:

<?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; 
} 

Nel mio documento PHP che io chiamo la seguente funzione:

if ($userid == -1) 
    { 
     showNotAuthorizedPage(); 
    } else { 
     myAccountPage(); 
    } 

e myAccountPage() funzione è dichiarata nello stesso file come il getUserPicPath() funzione, questo getUserPicPath() funzione viene richiamata come segue:

<div id="tabs-2"> 
    <p><?php getUserPicPath(); ?></p> 
    </div> 

uso le linguette (0.123.021,785086 millions) sulla mia pagina web ed è qui che voglio chiamare in La funzione myAccountPage(), che dà il seguente errore:.

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

Questa non è l'informazione completa disponibile. Proprio come controlli gli errori di connessione, dovresti controllare anche gli errori di query. Non ho familiarità con mysqli ma se apri il manuale troverai cose con 'error' sul suo nome. –

+0

Perché stai eseguendo la tua query in 'getUserPicPath()' due volte? Cosa ti dà l'eco dopo la prima chiamata? – andrewsi

+1

Vorrei anche controllare per vedere cosa c'è in 'mysqli_error()', in modo da poter vedere quale errore il database sta passando indietro – andrewsi

risposta

12

Penso che sia perché quando si chiude la connessione al database per la prima volta, si dimentica di fare:

unset($connection); 

E poi, quando si tenta la connessione al database di nuovo, craps fuori perché è ancora impostato sulla connessione chiusa.

-2

Hai dimenticato di includere la connessione al database. Basta aggiungere il $connection alla query sql:

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

    // enter code here 
} 
Problemi correlati