mysqli expects parameter 1 to be mysqli, null given

Problems with the Windows version of XAMPP, questions, comments, and anything related.

mysqli expects parameter 1 to be mysqli, null given

Postby jazkat » 21. December 2010 01:27

I am having problems with PHP mysqli functions recognizing my database. I keep getting the following error:
"Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\guardsman\trunk\prod\_swf\data\register.php on line 122"

"Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\guardsman\trunk\prod\_swf\data\register.php on line 217
Could not connect to MySQL:"

The typical cause of this error is placing the parameters in the wrong order. That doesn't seem to be the case here.
I have tried everything I can think of to get rid of these errors: I've tried to validate my PHP syntax (Aptana doesn't seem to have a problem with it but I'm not ruling it out as the cause???). I have no problem accessing the XAMPP installation of MySQL (which is XAMPP for Windows v. 1.7.3, MySQL v. 5.1.41). I run PHPMyAdmin and MySQL Workbench flawlessly. I've run my queries through PHPMyAdmin and MySQL Workbench to validate the syntax -- to be sure they are actually retrieving data from the tables as designed (they work as typed--without the PHP vars of course). But, when I try to use the queries in PHP I get the errors. I've done a var_dump($dba) on the line before the mysqli functions and I it returns NULL, but I can access the database and the server is running. I've run out of variables in this equation and can only assume that the errors are due to some settings in XAMPP (i.e. MySQL or PHP config files???).

Anyone have any ideas? Any help, suggestions, opinions, criticisms, or educated guesses would be greatly appreciated.

Kurt

Code: Select all
<?php
// This is my database access definition for the variable $dba
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('DB_NAME', 'training');

$dba = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
   OR exit('Could not locate the database from dbconnect.php:'.mysqli_connect_error());
// END $dba


// These are the functions that have the mysqli functions that throw the errors
function checkAccess($accCode){
   // *** ERROR line 122
   $cleanCode = mysqli_real_escape_string ($dba, $accCode); // *** ERROR line 122
   list($found, $data) = accessCodeQuery($cleanCode);
   if($found){
      if(openSeats($data['acc_seats'],$data['co_id'],$data['acc_id'])){
         if(isAccessCodActive($cleanCode)){
            $_SESSION['co_id'] = $data['co_id'];
            $_SESSION['acc_code'] = $data['acc_code'];
            $_SESSION['uty_id'] = $data['uty_id'];
            $_SESSION['co_name'] = $data['co_name'];
            $_SESSION['cont_location'] = $data['cont_location'];
            $_SESSION['cont_id'] = $data['cont_id'];
            $_SESSION['acc_id'] = $data['acc_id'];
            $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);
            
            if(getLessonMod($data['co_id'])){
               $errorMsg[] = "Success!";
               return array(true,$errorMsg);
            }else{
               $errorMsg[] = "Could not retrieve lesson/module data from database.";
               return array(false,$errorMsg);
            }
         }else{
            $errorMsg[] = "Your access code has expired. Please re-enter the code or contact you company's training representative.";
            return array(false,$errorMsg);
         }
      }else{
         $errorMsg[] = "The number of seats assigned to your access code are filled. Please re-enter the code or contact you company's training representative.";
         return array(false,$errorMsg);
      }
   }else{
      $errorMsg = $data;
      return array(false,$errorMsg);
   }
}



function openSeats($availSeats,$coId,$accId){
   if($availSeats != 9999){
      $query = "SELECT COUNT(user.u_id) AS usercount
            FROM company
            LEFT JOIN user
            ON company.co_id = user.co_id
            WHERE company.acc_id = '$accId'";      
// *** ERROR line 217
      $result = mysqli_query($dba, $query) OR exit("Could not connect to MySQL:".mysqli_connect_error());
      $rows = mysqli_fetch_array ($result, MYSQLI_ASSOC);
// Don't know if this works. Need to test with dummy data
      if ($rows['usercount'] >= $result) {
         return false;
         mysqli_close($dba);
      } else {
         return true;
         mysqli_close($dba);
      }
   }else{
      return true;
   }
   mysqli_free_result($result);
}?>
jazkat
 
Posts: 3
Joined: 21. December 2010 01:05

Re: mysqli expects parameter 1 to be mysqli, null given

Postby Altrea » 21. December 2010 06:14

You don't run into your own exit(), so i assume the connection can be established. But to be sure, you can var_dump() $dba right after the database connection function (should return an mysqli object)

We don't see the full script so i can't really solve your problem. BUT for some reason you lost your $dba object. I don't know if you've forgotten to transfer your object to another class or you overwrite your $dba variable or if any include fails where your $dba variable is in it (maybe because of short_open_tags which could be solved by php configuration, but i assume that the problem is something else). Thats your debugging homework for today :lol:

Check where in your script the connection is still okay and where it's no longer.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: mysqli expects parameter 1 to be mysqli, null given

Postby Nobbie » 21. December 2010 11:14

This line is wrong:

Code: Select all
$result = mysqli_query($dba, $query) OR exit("Could not connect to MySQL:".mysqli_connect_error());


That line is written inside a function, but the variable $dba is not known inside the scope of a function, as it as a global variable (defined outside the function). You either have to pass the variable down to the function or must define it as "global $dba" inside the function or use the array GLOBALS instead: $GLOBALS['dba']

And read the documentation about variables scope in PHP.
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: mysqli expects parameter 1 to be mysqli, null given

Postby jazkat » 21. December 2010 17:35

Thanks for the suggestions. I will try them and see what happens.
I had considered the possibility that $dba was out of scope but thought that, because it was in an include file that its scope was more broad. I will read through the variable scope documentation again, and try passing it directly into the functions.
jazkat
 
Posts: 3
Joined: 21. December 2010 01:05

Re: mysqli expects parameter 1 to be mysqli, null given

Postby jazkat » 21. December 2010 19:05

Thanks Nobbie for reminding me to evaluate the scope of my variables. That was the problem--I over estimated the scope of my $dba variable. Evidence that no matter how long you do this you can make really amateur mistakes if you don't sleep now and then.
:oops:
jazkat
 
Posts: 3
Joined: 21. December 2010 01:05


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 106 guests