Jump to content

Recommended Posts

Hey all,  New to the forum. I'm learning PHP MySQL so I'm sorry if this is something thats been brought up before.

 

I'm using the O'Reilly "Learning PHP & MySQL" book and I've come to the section about using PEAR's DB module.  They provided a 30 or so line of code to test if DB was installed and I'm getting an error.  But in my phpinfo.php file it says PEAR is enabled so I'm at a loss for conclusions.  Any help would be great.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php

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

$connection = DB::connect("mysql://$db_username:$db_password@db_host:db_database");

if (DB::isError($connection)){
die("Could not connect to the database: <br />".DB::errorMessage($connection));
}

$query = "SELECT * FROM 'books' NATURAL JOIN 'authors'";
$result = $connection->query($query);

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();

?>
</body>
</html>

 

PHP Info:

http://www.blankdiscmedia.com/newhtml/phpinfo.php

 

The Page:

http://www.blankdiscmedia.com/newhtml/db_test.php

 

The Error:

Warning: require_once(DB.php) [function.require-once]: failed to open stream: No such file or directory in /home/bdmedia/public_html/newhtml/db_test.php on line 12

Fatal error: require_once() [function.require]: Failed opening required 'DB.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/bdmedia/public_html/newhtml/db_test.php on line 12

 

Thanks from a php newbie

 

Tim

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/
Share on other sites

Where do I get DB.php from?  Maybe I'm just not reading this right but O'Reilly makes it sound like a Class thats included in PEAR and I'm just referencing it.  Seriously they just bring it up without what seems to be an instruction on how its supposed to get in my folder in the first place.

 

This is important to me because I'm trying to learn on my own with the book and the book uses this "DB" class to access the database in all its examples for the rest of the book. ???

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-217636
Share on other sites

This is what I found in the book

 

The file DB.php is found in the /pear subdirectory of the PHP distribution.  The PEAR install should have added that directory to the include_path in the php.ini file.  If this file is not found, verify that PEAR DB is installed and that the paths are set up correctly.

 

This isn't a file I'm supposed to create myself, it's a file PEAR provides with a class of DB within it that has predefined connect functions.

 

So if PEAR is installed on my server then the php.ini file must not have the right include_path?

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-217862
Share on other sites

I guess I just don't understand if using DB.php is so common in PHP coding and writing database query scripts why can nobody answer my question?

 

Is the DB class and fetchRow function commonly used or is the mysql_connect command more popular?

 

I'm just looking for somebody who uses the DB class to tell me how they know the file is there or where I can get it or what it does or anything. Or if DB is working but I haven't created a proper DB file, what is the script I need to put in the DB file?

 

I appreciate the workarounds, but I would like to not do a workaround, I want DB to work, it seems the rest of the code works just fine except for trying to access the DB class. :-\

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-218169
Share on other sites

update:

 

my server guy reinstalled the PEAR DB package, added a path to php.ini and now I get the error:

 

Fatal error: Class 'DB' not found in /home/bdmedia/public_html/newhtml/db_test.php on line 14

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<?php

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

$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");

if (DB::isError($connection)){
die("Could not connect to the database: <br />".DB::errorMessage($connection));
}

$query = "SELECT * FROM 'books' NATURAL JOIN 'authors'";
$result = $connection->query($query);

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();

?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-218462
Share on other sites

because I'm learning from a book that uses it, i'm trying to do the same connection functions using mysql_connect and I'm having some success, but that doesn't mean I shouldn't learn DBs more compact functionality for farther down the road does it?

 

I realize that this particular code is simple, but thats the point its simply a test to see if DB is installed and working correctly so for the rest of the book I can work with the DB class to connect and call fetchrow functions and whatnot and follow along.

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-219224
Share on other sites

sarcasm is always appreciated. thanks for the derailment, it was very helpful, if anything at least it bumped my thread a few times.

 

Is O'Reilly not the standard for learning PHP? I recall a few classes in college that used O'Reilly's books, sorry if the book doesn't live up to your standards.

 

I don't see how my servers settings have anything to do with my book or my code.

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-219356
Share on other sites

I'm an O'Reilly fan.. but skipping chapters doesn't work.. plus the Pear chapter does state

in addition to database access and templates, we also explain how to install PEAR packages and .....

 

in addition

 

to your comment

I don't see how my servers settings have anything to do with my book or my code.

 

again from the O'Reilly book about PEAR

if you find that you PHP engine can't find DB.php, it's likely that your include_path directive in your php.ini configuration file doesn't include the PEAR directory, Check the installation .....

 

 

so i say again read the chapter again or any you missed

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-219380
Share on other sites

I think you need to read up on error codes, as it tells you exactly what is wrong, and where:

 

this is line 14:

 

$connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");

 

This is the error:

 

Fatal error: Class 'DB' not found in /home/bdmedia/public_html/newhtml/db_test.php on line 14

 

My **guess** would be that you are calling an undefined class (DB) with the command DB::connect. Find out where that class is supposed to be defined, and include that file into this script.

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-219389
Share on other sites

I'm an O'Reilly fan.. but skipping chapters doesn't work.. plus the Pear chapter does state

in addition to database access and templates, we also explain how to install PEAR packages and .....

 

I'm using an ISP with this already installed. I didn't skip the chapter, I don't know how to control my PEAR list on my ISP host, my server guy already changed the include path in the php.ini file.  Furthermore O'Reilly doesn't say how to log into an ISP using Terminal to install PEAR or adjust its settings, it only has instructions for local installation, so sorry I can't pull that info out of nowhere, i've already stated I'm a beginner learning from scratch.

 

in addition

 

to your comment

I don't see how my servers settings have anything to do with my book or my code.

 

again from the O'Reilly book about PEAR

if you find that you PHP engine can't find DB.php, it's likely that your include_path directive in your php.ini configuration file doesn't include the PEAR directory, Check the installation .....

 

In my first error it was saying that DB.php was not found, after we switched the include_path now we get the Fatal Error which is completely different as far as I know, which is my current question.  What is this error or how is it the same as the file DB.php not found?

 

so i say again read the chapter again or any you missed

 

I can read it 4 more times than the 3 I already have, just because I came here for auxilary support and understanding doesn't mean I skipped chapters.

 

 

To what yzerman said: Am I not understanding this right that the DB Class is pre-defined in DB.php within the DB package of PEAR?  Thats the whole reason PEAR is installed and used in the first place isn't it?

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-219471
Share on other sites

I'm pretty sure he did, but regardless hes going to reinstall php and pear today and check the php.ini include_path again and we're going to try it again tonight or tomorrow and see if the error shows up still.  I'll post back.  If that doesn't work I'll post on the Pear Forums and see if anyone there has any such difficulty with PEAR installs online.

Link to comment
https://forums.phpfreaks.com/topic/44793-dbphp-error/#findComment-219894
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.