2014-07-26 6 views
11

नहीं लाया जा सकता है मुझे एक समस्या है जहां मैं अपने MySQL डेटाबेस (PHP के माध्यम से) से परिणाम पुनर्प्राप्त नहीं कर सकता। मैं अन्य कार्यों में एक ही फ़ंक्शन का उपयोग करता हूं और यह बेकार ढंग से काम करता है। हालांकि इस बिंदु पर मैं "चेतावनी: mysqli_query() प्राप्त कर रहा हूं: mysqli" त्रुटि नहीं लाया जा सका। समस्या का विवरण नीचे समझाया गया है। मैं कहीं और एक काफी समान फ़ंक्शन का उपयोग करें (getAllCountries नीचे के रूप में देखा जाता है) मेरी PHP में पूरी तरह से काम करता है जो:चेतावनी: mysqli_query(): mysqli

मैं एक php निम्नलिखित कोड युक्त फ़ाइल है:

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

तो समस्या निम्नलिखित है:

require 'dbfunctions.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 फ़ाइल निम्न पंक्ति (एक साथ अन्य गैर प्रासंगिक कार्यों के साथ) है

और मेरे 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; 
} 

मेरी पीएचपी दस्तावेज़ मैं निम्नलिखित समारोह फोन में:

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

और myAccountPage() समारोह के रूप में एक ही फाइल में घोषित किया जाता है getUserPicPath() फ़ंक्शन, इस getUserPicPath() समारोह के रूप में कहा जाता है इस प्रकार है:

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

मैं टैब का उपयोग (मेरी वेब पेज पर) और वह यह है कि मैं कहाँ में यह कॉल करना चाहते हैं 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 से परिचित नहीं हूं लेकिन यदि आप मैन्युअल खोलते हैं तो आपको इसके नाम पर 'त्रुटि' के साथ सामान मिल जाएगा। –

+0

आप 'getUserPicPath() 'में दो बार अपनी क्वेरी क्यों चला रहे हैं? पहली कॉल के बाद 'echo' क्या आपको देता है? – andrewsi

+1

मैं यह देखने की भी सिफारिश करता हूं कि 'mysqli_error()' में क्या है, ताकि आप देख सकें कि डेटाबेस – andrewsi

उत्तर

12

मुझे लगता है कि यह है, क्योंकि जब आप डेटाबेस कनेक्शन पहली बार बंद करते हैं, आपको बस इतना करना भूल जाते हैं:

unset($connection); 

और फिर जब आप फिर से डेटाबेस से कनेक्ट करने की कोशिश है, यह बाहर क्रेप्स, क्योंकि यह अभी भी बंद कनेक्शन को तैयार है।

-2

आप अपना डेटाबेस कनेक्शन शामिल करना भूल गए हैं। बस अपने एसक्यूएल क्वेरी में $connection जोड़ें:

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

    // enter code here 
} 
संबंधित मुद्दे