2

Fatal error: Call to a member function query() on a non-object on line: $result = $conn->query($sql) or die(mysqli_error());

Who knows whats wrong and how to fix it?

<?php
function dbConnect($usertype, $connectionType = 'mysqli') {
  $host = 'localhost';
  $db = 'phpsols';
  if ($usertype  == 'read') {
    $user = 'psread';
    $pwd = '123';
  } elseif ($usertype == 'write') {
    $user = 'pswrite';
    $pwd = '123';
  } else {
    exit('Unrecognized connection type');
  }
  if ($connectionType == 'mysqli') {
    return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');
  } else {
    try {
      return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
    } catch (PDOException $e) {
      echo 'Cannot connect to database';
      exit;
    }
  }
}

// connect to MySQL
$conn = dbConnect('read');
// prepare the SQL query
$sql = 'SELECT * FROM images';
// submit the query and capture the result
**$result = $conn->query($sql) or die(mysqli_error());**
// find out how many records were retrieved
$numRows = $result->num_rows;
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Connecting with MySQLi</title>
</head>

<body>
<p>A total of <?php echo $numRows; ?> records were found.</p>
</body>
</html>
tereško
  • 58,060
  • 25
  • 98
  • 150
easyrider
  • 701
  • 3
  • 10
  • 20

2 Answers2

9

The culprit is most likely this line:

return new mysqli($host, $user, $pwd, $db) or die ('Cannot open database');

The do xyz or die() construct leads to funny behaviour in conjuction with the return statement (i.e. the whole thing is interpreted as an OR expression and because new mysqli will never be false, the "die" is never processed.). See a similar case here.

Do this instead:

$result = new mysqli($host, $user, $pwd, $db) ;
if (!$result) die (....);
return $result;

Also, slightly related, I think you will never catch a PDO connection error because this:

return new PDO("mysql:host=$host;dbname=$db", $user, $pwd);

will always exit the function, and never reach the catch block. As with your actual problem, the solution is to pass the object to a $result variable first.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • But, if I understand this well, that line is not being executed, he is calling the function without the second argument. – AJJ Jan 08 '11 at 12:23
  • @AJweb which line do you mean? Which function? – Pekka Jan 08 '11 at 12:24
  • My apologies, didn't realise the "mysqli" is set as default. – AJJ Jan 08 '11 at 12:29
  • Pekka - do you maybe know why $conn and $result in my debugger (PHPED) are shown with an empty value ? – easyrider Jan 08 '11 at 12:54
  • @easyrider hmm, they shouldn't be empty if they contain a valid DB object - no idea why this would happen – Pekka Jan 08 '11 at 12:55
  • Pekka - any suggestion which debugger would show me their real values? – easyrider Jan 08 '11 at 13:01
  • @easyrider No idea, sorry. Maybe ask this as a separate question, I don't know what's up with that – Pekka Jan 08 '11 at 13:05
  • Just got to this part in the book, I was lost until I found this. Thanks! +1 – Drewdin May 02 '11 at 02:35
  • Another one here who's very grateful for this. Surprised that the connection works and produces a result, but the 'do xx or die' nested in the return produces the error ??? still, great to have the solution here. – DBMarcos99 May 25 '11 at 10:08
5

I know this has been answered, but just wanted to add my own 2 cents worth....

I was googling "return new mysqli" to fix this very problem. The code is a snippet from an e-book (PHP Solutions. Dynamic Web Design Made Easy.) - 2nd Edition if I'm not too mistaken! Ironically the book is supposed to be teaching PHP basics (in this case DB connection methods) to poor souls like myself and easyrider. I have very limited understanding, as this book is my own introduction to PHP coding but I managed to suss out that creating or rather instantiating an actual object, passing it to a temporary variable and then returning that variable was the solution.

Annoyingly, this was demonstrated correctly in the first edition of said publication.

I dont know if Google will cache or index my post, but it might help others who are learning from the book, to discover that the code example is flawed, and as the time of writing, the error is not listed in the errata on the publishers website.

XLCR
  • 51
  • 1
  • 1
  • For those using PHP Solutions - Dynamic Web Design Made Easy Version 2, The publisher issued their correction, Check it out [here](http://foundationphp.com/phpsolutions/errata_2e.php). Pekka is right on! –  Jul 27 '12 at 15:18