Page 1 of 1

DB::connect and/or PEAR problem

PostPosted: 24. June 2008 04:24
by vbcubey
I'm having trouble connecting to a MySQL database using PEAR's DB package. I'm using the following code, which is part of a tutorial in the book I'm using to learn PHP/MySQL:

Code: Select all
<?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));
}

?>


The error I get when I run the code is:

Code: Select all
Could not connect to the database:
connect failed


I can connect to the database just fine using mysql_connect(); this happens only when I use PEAR.

I'm running xampp 1.6.6a, and I have Windows XP SP3. I looked through similar situations in the forum, and closest I could find was an MDB2 pathing issue, which wasn't similar enough for me to adapt the answer.

I'm very new at PHP, but I'll try my best with any help you can give me.

PostPosted: 24. June 2008 11:10
by Wiedmann
What's the content of "db_login.php"?

(BTW: Why did you use PEAR::DB and not PEAR::MDB2?)

PostPosted: 24. June 2008 14:33
by vbcubey
The contents of db_login.php:

Code: Select all
<?php
$db_host='localhost';
$db_database='authors';
$db_username='vbcubey';
$db_password='********';
?>


Instead of ********, the actual password is listed in the file.

I'm using PEAR::DB because the book I'm learning from uses it first. I don't know enough to know the difference between the two (DB and MDB2) so I thought I should stick with this example until I understand more.

There is an MDB2 example later in this book too, but I didn't see the point of going on to that one if the DB example doesn't work.

PostPosted: 24. June 2008 14:53
by Wiedmann
because the book I'm learning from uses it first.

That's the problem with most/many PHP books. Old and using outdated code...

Code: Select all
$connection = DB::connect("mysql://$db_username:$db_password@db_host/$db_database");
                                                           --^

a "$" is missing.

BTW:
If you replace your code with this, you could see the error:
Code: Select all
<?php
error_reporting(E_ALL);

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

$connection =& DB::connect("mysql://$db_username:$db_password@db_host/$db_database");
if (PEAR::isError($connection)) {
    echo $connection->getMessage().'<br />';
    echo $connection->getDebugInfo();
    exit;
}
?>

And after a first test, replace "@db_host" with "@$db_host"...

PostPosted: 24. June 2008 15:41
by vbcubey
Yep, that $ was all I needed. Thank you very, very much.

Since all of the books I see are at least a year old, can you recommend a site that can get me up-to-date on new coding practices?