Hello,
I have been trying to bring my PHP skills up to date, and have been
reading a few O'Reilly books and pages on the internet. One recurring
theme I've noticed is people using PEAR's DB package to access MySQL
databases, so I decided to give it a try. However, I cannot get the
error reporting method to return an error, even when I pass a query
which I -know- should not work. Below is a sample of code I'm trying
to get to work. The table nonexistant is, of course, nonexistant, and
therefore trying to query it should return an error.
<?php
require_once "DB.php";
require '/home/devel/db_info.php';
$dsn = "mysql://{$db_user}:{$db_pass}@{$db_server}/{$db_name}";
$connection = DB::connect($dsn);
if (DB::isError($connection))
die($connection ->getMessage( ));
$result = $connection->query("SELECT * FROM nonexistant");
if (DB::isError($result))
die($result->getMessage( ));
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
foreach($row as $attribute)
print "{$attribute} ";
print "\n";
}
?>
You can see the result of this script being run at:
http://devel.introversion.net/php5/pear_test.php
I have tried using sample code from several different sources, in many
cases simply copying and pasting it into a file, and the isError()
method still won't return anything for me. There shouldn't be
anything wrong with the code, since I basically copied it out of a
book. db_info.php is a file containing the login information for the
database, and the script is having no problem accessing that.
Additionally, if I have the script run a legitimate query, that works
properly.
However, if you can find something wrong with the code that is causing
this, I would really appreciate it. Otherwise, I'd just like any
suggestions you have as to what else might be configured wrong on the
server. I have root access to the machine and will be able to make
any modifications necessary.
Thanks in advance. |
Request for Question Clarification by
googleexpert-ga
on
31 Oct 2004 07:48 PST
Hi peterallen-ga,
I ran your code sample and and could not find any problems with it.
What I got back was:
DB Error: no such table
Have you checked to see if PEAR is installed in the right location?
Please use phpinfo() to look for your include path.
Once, you have found your include, check to see if directory/folder "PEAR" exists.
<? /*info.php*/
phpinfo();
?>
Hope to hear from you soon.
-googleexpert
|
Clarification of Question by
peterallen-ga
on
31 Oct 2004 14:55 PST
The configure command used to compile PHP as listed by phpinfo()
included '--with-pear'. A whereis of 'pear' on that machine shows
that it is installed at /usr/local/bin/pear. Additionally, 'ls
/usr/share/pear' shows that DB.php is installed on the system.
Finally, the script includes DB.php by 'require_once' which should
cause the script to fatally error if the file cannot be found. So, as
far as I know, Pear is installed properly, and the script is not
having trouble finding the necessary file(s). Also, the methods that
are created in DB.php work throughout the script, with the exception
of error reporting. i.e., I can use PEAR DB to access and modify my
databases, it just fails to report any errors it encounters.
However, I'm new with using Pear, so I could be overlooking something.
The output of phpinfo() can be found here:
http://devel.introversion.net/php5/phpinfo.php
Thanks for looking into this for me.
|
Request for Question Clarification by
googleexpert-ga
on
31 Oct 2004 20:24 PST
I noticed you're using php5.
Would it be possible for you to test PEAR DB with php4?
Also, there is a function that sets the type of error handling, namely:
"setErrorHandling"
<?php
require_once "DB.php";
require '/home/devel/db_info.php';
$dsn = "mysql://{$db_user}:{$db_pass}@{$db_server}/{$db_name}";
$connection = DB::connect($dsn);
/* Over Here!!!!*/
$connection->setErrorHandling(PEAR_ERROR_RETURN);
/* Over Here!!!!*/
if (DB::isError($connection))
die($connection ->getMessage( ));
$result = $connection->query("SELECT * FROM nonexistant");
if (DB::isError($result))
die($result->getMessage( ));
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
foreach($row as $attribute)
print "{$attribute} ";
print "\n";
}
?>
Please test the code I have posted to see if Error Reporting works.
Thank you.
-googleexpert
|
Clarification of Question by
peterallen-ga
on
31 Oct 2004 21:23 PST
I added that line, and it did not fix the problem. :/
I could replace PHP5 with PHP4, but unfortunately that would defeat
the purpose of my project, which is to re-write my website using PHP5.
I know that PHP5 is not entirely stable yet, but from what I can
tell, it should play nice with PEAR, and I have tired myself out
trying to find even one example of someone else having the same
problem, and I've come up with nothing. Even if this is being caused
by a problem which I cannot fix (such as an incompatibility with PEAR
DB and PHP5), i would like to at least isolate that problem so I know
what it is, work around it, and wait for a fix in the future.
Thanks again for your help so far. Please let me know if you come up
with anything else to try, or find anything that might explain this
problem.
|
Clarification of Question by
peterallen-ga
on
31 Oct 2004 21:59 PST
Also, I think it is worth mentioning that much of the code I have
tried has been taking from books specifically written for PHP5, so it
really shouldn't be a PHP5 compatibility problem.
|
Clarification of Question by
peterallen-ga
on
01 Nov 2004 08:54 PST
On someone's advice, I tried adding print_r($result), and the output
shows that indeed an error object is being returned, so the problem
lies in the fact that DB::isError() is not returning "true" when an
error object is passed to it. Or at least that's what I understand it
to mean. Still can't figure out why.
|
Request for Question Clarification by
googleexpert-ga
on
01 Nov 2004 19:43 PST
Hi again,
My only other suggestion is to comment the first "die" call:
<?php
require_once "DB.php";
require '/home/devel/db_info.php';
$dsn = "mysql://{$db_user}:{$db_pass}@{$db_server}/{$db_name}";
$connection = DB::connect($dsn);
# if (DB::isError($connection))
# die($connection ->getMessage( ));
$result = $connection->query("SELECT * FROM nonexistant");
if (DB::isError($result))
die($result->getMessage( ));
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
foreach($row as $attribute)
print "{$attribute} ";
print "\n";
}
?>
I have also read some of the PHP5 books you mentioned;
You are right, most of your sample code is from PHP5 books with some
variable name changes.
It's a mystery to me why your sample code doesn't work.
Can you please post another link for phpinfo?
The one you gave:
http://devel.introversion.net/php5/phpinfo.php
doesn't work at all.
Thank you.
-googleexpert
|
Clarification of Question by
peterallen-ga
on
02 Nov 2004 05:51 PST
I tried commenting those lines. No luck. Sorry about posting the
wrong URL to the phpinfo() page. The correct address is:
http://devel.introversion.net/php5/php_info.php
Also, you can see highlighted source code of pear_test.php at
http://devel.introversion.net/php5/pear_test.phps
Thanks
|
Request for Question Clarification by
googleexpert-ga
on
02 Nov 2004 06:18 PST
Hi again,
There's a couple of code edits you might want to try.
<?php
error_reporting (E_ALL ^ E_NOTICE);
require_once "DB.php";
require '/home/devel/db_info.php';
$dsn = "mysql://{$db_user}:{$db_pass}@{$db_server}/{$db_name}";
$connection = DB::connect($dsn);
if (DB::isError($connection))
die($connection ->getMessage( ));
$result = $connection->query("SELECT * FROM nonexistant");
if (DB::isError($result))
die($result->getMessage( ));
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
foreach($row as $attribute)
print "{$attribute} ";
print "\n";
}
?>
You will also want to read the section about "Debugging PEAR DB Errors" at:
http://vulcanonet.com/soft/?pack=pear_tut#ss5.2
"Normaly when a PHP function fails, it will print an error message. In
Pear these behaviour has been disabled. But perhaps sometimes you'll
need to see these messages to trap obscure errors in your code. This
can be done with the set_error_handler PHP function, documented in the
PHP Manual. Here is a quick example:"
<?php
// what messages to report
error_reporting (E_ALL ^ E_NOTICE);
// this function will handle all reported errors
function my_error_handler ($errno, $errstr, $errfile, $errline) {
echo "In $errfile, line: $errline\n<br>$errstr";
}
set_error_handler ('my_error_handler');
$db = DB::connect('pgsql://postgres@localhost/no_db');
...
?>
Hope that helps you.
-googleexpert
|
Clarification of Question by
peterallen-ga
on
03 Nov 2004 21:08 PST
I added that code for the error handler. It does print a nice list of
errors and warnings, but I'm not able to find the cause of the problem
from these errors. The only thing that stands out to me is:
Non-static method DB::isError() should not be called statically
but I'm not sure what that means or how to fix it. Hopefully you can
make more sense of it than I can.
|
Request for Question Clarification by
googleexpert-ga
on
03 Nov 2004 22:47 PST
I can't shed any light on the non-static method issue.
Coincidentally, someone else also has this unsolved problem:
[Source: http://www.zend.com/phorum/read.php?num=4&id=2031&thread=1928]
My last suggestion, is for you to download and then use php(v 5.0.0) at:
http://us2.php.net/releases.php
...since, the O'Reilly books you read was probably not written with
the latest php(v 5.0.2)
good luck on your php journey.
-googleexpert
|