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
Lphp
Forum Commoner
Posts: 74 Joined: Sun Jun 26, 2011 9:56 pm
Post
by Lphp » Mon Sep 19, 2011 5:11 am
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>
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Mon Sep 19, 2011 2:51 pm
dbConnect() is not returning an object for some reason. What is the return value?
(#10850)
twinedev
Forum Regular
Posts: 984 Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio
Post
by twinedev » Mon Sep 19, 2011 2:52 pm
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
Post
by Lphp » Mon Sep 19, 2011 8:52 pm
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');
twinedev
Forum Regular
Posts: 984 Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio
Post
by twinedev » Tue Sep 20, 2011 12:43 am
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
Post
by Lphp » Wed Sep 21, 2011 3:43 am
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!
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Wed Sep 21, 2011 6:48 am
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
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
Post
by Lphp » Wed Sep 21, 2011 8:57 pm
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
Post
by Lphp » Wed Sep 21, 2011 9:14 pm
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!