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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

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

Post 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>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

dbConnect() is not returning an object for some reason. What is the return value?
(#10850)
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post by twinedev »

if you do var_dump($conn); on the line before you try to do the query, what do you get?
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

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

Post 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');
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post by twinedev »

twinedev wrote:if you do var_dump($conn); on the line before you try to do the query, what do you get?
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

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

Post 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!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

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

Post 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
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

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

Post 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!
Post Reply