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