tags:

views:

41

answers:

1

Hello, 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 server, 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

+3  A: 

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;
}
Peter Bailey
thx Peter, a few of my tables had locked up :S now all is well, i will use this structure to help catch errors better/quicker in the future, thanks muchos :)Aaron
Aaron Bentley