Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PEAR DB Connect Error

Status
Not open for further replies.

tweenerz

Programmer
Mar 25, 2002
202
US
I am having trouble getting the PEAR DB or MDB2 classes to connect to my db, while the native mysql db functions connect fine.
The following connection method works fine ...
Code:
$hostname='myhost.com';
$username='user;
$password='foo';
$dbname='bar';
$usertable='foobar';
$yourfield = 'id';

mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);

$query = "SELECT * FROM $usertable";
$result = mysql_query($query);
if($result) {
    while($row = mysql_fetch_array($result)){
        $name = $row[$yourfield];
        echo 'Name: '.$name;
    }
}
OUTPUT: [highlight][tt]Name: 1[/tt][/highlight]
However, the following triggers an error:
Code:
$hostname='myhost.com';
$username='user;
$password='foo';
$dbname='bar';
$usertable='foobar';
$yourfield = 'id';
require_once("DB.php");
$dburl = "mysql://$username:@$password@$hostname:3306/$dbname";
$con = DB::connect($dburl);

if(PEAR::isError($con)) {
    [b]die("Error while connecting : " . $con->getMessage());[/b]
}
$sql = "SELECT * FROM Expenses";$resultset = $con->query($sql);
if(PEAR::isError($resultset)) {
    die('Failed to issue query, error message : ' . $resultset->getMessage());
}

while($row = $resultset->fetchRow(MDB2_FETCHMODE_ASSOC)) {
    foreach($row as $field => $value) {
        echo "$key / $value \n";
    }
}

The error is thrown where the bold line is:
OUTPUT: [highlight][tt]Error while connecting : DB Error: connect failed[/tt][/highlight]

So basically, the native mysql db functions are able to connect, but using PEAR DB class, or the MDB2 class for that matter (I've tried them both), causes an error. As you can see, I am using variables for the connection, so the credentials are not an issue

One thing to note is that this server doesn't have pear installed, so I am including it locally which I have done plenty of times with no problem, but not sure if it would cause an error here. It obviously is using the PEAR libraries fine and I did update the include path to have the local pear directory in it. Also, I have error_reporting set to E_ALL and display_errors set to 1 and am not getting any other errors.

any help is greatly appreciated.

Thanks
 
One more note, the MDB2 error that gets thrown is:
[highlight]Error while connecting : MDB2 Error: not found[/highlight]

which is slightly different and maybe more specific.

Thanks
 
Hi,
I run into the same problem today. Did you get a solution in the meantime?

Thanks
 
Yes. The mysql driver wasn't included. This was the error:

unable to find package 'MDB2_Driver_mysql' file 'MDB2/Driver/mysql.php'

I had to find the drivers and place them in the Driver directory.

Hope that helps.
 
Thank You, that was it. Now it works

rxf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top