php - Warning: mysqli_query(): Couldn't fetch mysqli -


i have problem can not retrieve result mysql database (via php). use same function in other places , works flawlessly. @ point keep getting "warning: mysqli_query(): couldn't fetch mysqli" error. details of problem explained below. use quite similar function elsewhere (getallcountries seen below) in php work perfectly:

function getallcountries() {     $result = db_query("select countryid, name country order 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()); } 

so problem following:

i have php file containing following code:

<?php require 'includes/functions.php';  function getuserpicpath() {     $userid = $_session['userid'];      $result = db_query("select picture user userid='$userid'");      while($row = mysqli_fetch_array($result)) {         $picturepath = $row['picture'];     }      echo $picturepath;      mysqli_close(db_connect()); } 

my functions.php file has following line (together other non-relevant functions):

require 'dbfunctions.php'; 

and dbfunctions.php looks this:

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

in php document call following function :

if ($userid == -1)     {         shownotauthorizedpage();     } else {         myaccountpage();     } 

and myaccountpage() function declared in same file getuserpicpath() function, getuserpicpath() function called follows:

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

i use tabs (http://jqueryui.com/tabs/#default) on webpage , want call in. myaccountpage() function gives following error :

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

i think because when close database connection first time, forget do:

unset($connection); 

and when try connecting database again, craps out because still set closed connection.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -