Page 1 of 1

Fatal error: Call to a member function query() on a non-obje

Posted: Mon Sep 19, 2011 5:11 am
by Lphp
I try the following example , but get Fatal error: Call to a member function query() on a non-object How to fix the problem??

Code: Select all

<?php
function dbConnect($usertype, $connectionType = 'mysqli') {
  $host = 'localhost';
  $db = 'test';
  if ($usertype  == 'read') {
	$user = 'psread';
	$pwd = 'read12';
  } elseif ($usertype == 'write') {
	$user = 'pswrite';
	$pwd = 'write12';
  } 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;
    }
  }
}

Code: Select all

<?php
require_once('../includes/connection.inc.php');
// 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) ;
// 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>
<table>
  <tr>
    <th>image_id</th>
    <th>filename</th>
    <th>caption</th>
  </tr>
<?php while ($row = $result->fetch_assoc()) { ?>
  <tr>
    <td><?php echo $row['image_id']; ?></td>
    <td><?php echo $row['filename']; ?></td>
    <td><?php echo $row['caption']; ?></td>
  </tr>
<?php } ?>
</table>
</body>
</html>

Re: Fatal error: Call to a member function query() on a non-

Posted: Mon Sep 19, 2011 2:51 pm
by Christopher
dbConnect() is not returning an object for some reason. What is the return value?

Re: Fatal error: Call to a member function query() on a non-

Posted: Mon Sep 19, 2011 2:52 pm
by twinedev
if you do var_dump($conn); on the line before you try to do the query, what do you get?

Re: Fatal error: Call to a member function query() on a non-

Posted: Mon Sep 19, 2011 8:52 pm
by Lphp
Christopher wrote:dbConnect() is not returning an object for some reason. What is the return value?
according the code , it suppose return

Code: Select all

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

Re: Fatal error: Call to a member function query() on a non-

Posted: Tue Sep 20, 2011 12:43 am
by twinedev
twinedev wrote:if you do var_dump($conn); on the line before you try to do the query, what do you get?

Re: Fatal error: Call to a member function query() on a non-

Posted: Wed Sep 21, 2011 3:43 am
by Lphp
twinedev wrote:
twinedev wrote:if you do var_dump($conn); on the line before you try to do the query, what do you get?
I don't get your suggestion or question!

Re: Fatal error: Call to a member function query() on a non-

Posted: Wed Sep 21, 2011 6:48 am
by Celauran
Your dbConnect should be returning a mysqli object, but it clearly isn't. We're trying to ascertain what is being returned instead. Since you're storing that return value in $conn

Code: Select all

var_dump($conn);
will allow us to see what is being returned, and we can work from there.

Re: Fatal error: Call to a member function query() on a non-

Posted: Wed Sep 21, 2011 8:57 pm
by Lphp
I still don't get what is your suggestion!
for My understand $conn = dbConnect('read'); $conn = dbConnect('read', 'mysqlli'); but still don't work
beside $conn = dbConnect('read', 'pdo'); is work I also post the connection code , I don't see the close connection is called

Re: Fatal error: Call to a member function query() on a non-

Posted: Wed Sep 21, 2011 9:14 pm
by Lphp
hi all
take away the or die ('Cannot open database') then everything is work, maybe php can't return by this way , can't anyone give more explanation!