error related to PEAR DB and oop

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
reson
Forum Newbie
Posts: 7
Joined: Sat Jun 30, 2007 8:06 pm

error related to PEAR DB and oop

Post by reson »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, I'm trying to get the PEAR DB installed, but I'm getting this error when I try it:

"Fatal error: Call to a member function query() on a non-object in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\pear_test.php on line 36"

My code is as follows (I commented line 36):

Code: Select all

<?php

include('db_login.php');
require_once('DB.php');

$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die ("Could not connect to the database: <br />". mysql_error());
}

$dbname = 'test';
$db_select = mysql_select_db($dbname);

if (!$db_select){
die ("could not select the database: <br />". mysql_error());
}

echo "connected to $dbname <br /><br />";
 
$query = "select * from books natural join authors";
$result = $connection->query($query); //<<<<<<<<<<<<<<<<<<<<< LINE 36

if (DB::isError($result)){
die ("could not query the database: <br />".$query." ".DB::errorMessage($result));
}

echo('<table border="1">');
echo '<tr><th>Title</th><th>Author</th><th>Pages</th></tr>';

while ($result_row = $result->fetchRow()){
	echo "<tr><td>";
	echo $result_row[1] . '</td><td>';
	echo $result_row[4] . '</td><td>';
	echo $result_row[2] . '</td></tr>';
	}

echo("</table>");
$connection->disconnect();

?>

Any ideas on what's causing the error? Thanks for any help.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

You are trying to use the variable $connection as though it were an object. It is not, it is a connection resource.
Post Reply