Errors on server! Need another set of eyes over simple php mysqli
i have the following php code selecting user details from a mysql db
$mysql = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database');
$stmt = $mysql->prepare('SELECT personalinfo.userid, personalinfo.firstname, personalinfo.lastname FROM personalinfo INNER JOIN applications ON personalinfo.userid = applications.userid WHERE applications.jobref = ?');
$stmt->bind_param('i',$jr);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($userID,$firstName,$lastName);
$count = $stmt->num_rows();
I have tested this code localy at it works fine, however when i try to test on a live serve开发者_Python百科r, i keep getting the following error "Fatal error: Call to a member function bind_param() on a non-object in C:\path\to\directory"
Thx in advance peeps,
Aaron
Any
Well, according to the documentation for mysqli::prepare(), it will return FALSE
if there was an error, which is why you're getting a "non-object" error when trying to call $stmt->bind_param()
since $stmt
isn't an object at this point.
So both for the sake of your current error and any future errors, make sure to check for falseness when creating a statement and act accordingly.
$mysql = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database');
$stmt = $mysql->prepare('SELECT personalinfo.userid, personalinfo.firstname, personalinfo.lastname FROM personalinfo INNER JOIN applications ON personalinfo.userid = applications.userid WHERE applications.jobref = ?');
if ( $stmt )
{
$stmt->bind_param('i',$jr);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($userID,$firstName,$lastName);
$count = $stmt->num_rows();
} else {
// something went wrong
// perhaps display the error?
echo $mysql->error;
}
精彩评论