Personal address book

I am doing a tutorial from a site (http://www.hosting.vt.edu/tutorials/phpmysql/#interactingwithdatabase) and keep getting an error message
Fatal error: Call to undefined method DB_Error::query() in C:\wamp\www\listcontacts.php on line 35
which is this: $result = $database->query($query);
[php]<?php
$query = "SELECT * FROM contacts ORDER BY ".$_SESSION[‘sort’];
$result = $database->query($query);
for ($i=0; $i<$result->numRows(); $i++) {
$contact = $result->fetchRow(DB_FETCHMODE_ASSOC,$i);
?>[/php]
the website has the code for this application, but I dont understand whats wrong with the line.

I think it maybe that the DB isn’t connecting for some reason (wrong username/password/ect.).

In the file dbconnect.php which has:

[php]<?php
// load the PHP PEAR database library
include(‘DB.php’);
// connect to the database, providing the DB user-ID, password, host name, DB
$connection = ‘mysql://dbuser42:uIW34#[email protected]/database42’;
$database = DB::connect($connection);
?>[/php]

try changing to:

[php]<?php

// load the PHP PEAR database library
include(‘DB.php’);
// connect to the database, providing the DB user-ID, password, host name, DB
$connection = ‘mysql://dbuser42:uIW34#[email protected]/database42’;
$database = DB::connect($connection);

if (DB::isError($database)) {
die($database->getMessage() . ‘
’ . $database->getDebugInfo());
}
?>
[/php]

Dose it output any errors?

it came up as
DB Error: not found
Unable to include the DB/mysql.php file for ‘mysql://root:@localhost/test’

Because I am running it on localhost with no password would mine look like this?

$connection = 'mysql://root:@localhost/test';

Your connection string looks fine.

The error is because it can’t find the file “DB/mysql.php”. There is some information about the error here: http://www.pear-forum.org/topic2071.html

Thanks I will take a look at it and see if I can get it to work later today.

From that sites final post what exactly did he move and change in the code? He said he moved php.ini file in root and added the include_path in that file.

[php]<?php
// load the PHP PEAR database library
include(‘DB.php’);

// connect to the database, providing the DB user-ID, password, host name, DB
$connection = ‘mysql://root:@localhost/test’;
$database = DB::connect($connection);

if (DB::isError($database)) {
die($database->getMessage() . ‘
’ . $database->getDebugInfo());
}
?>[/php]

so I should add this to it?

[code]ini_set(‘include_path’, ‘~/pear/lib’ . PATH_SEPARATOR . ini_get(‘include_path’));

// From PHP 4.3.0 onwards you can use, especially useful on a shared host
set_include_path(’~/pear/lib’ . PATH_SEPARATOR . get_include_path());
require_once(“v1.0/Classes/DB.php”); [/code]

You will need to find the right path on your computer. If you search for the file mysql.php and see where it is located. Once you have that you would do:
[php]<?php
ini_set(‘include_path’, ‘/path/to/mysql.php/’ . PATH_SEPARATOR . ini_get(‘include_path’));
// load the PHP PEAR database library
include(‘DB.php’);

// connect to the database, providing the DB user-ID, password, host name, DB
$connection = ‘mysql://root:@localhost/test’;
$database = DB::connect($connection);

if (DB::isError($database)) {
die($database->getMessage() . ‘
’ . $database->getDebugInfo());
}
?>[/php]

Would it be
[php]ini_set(‘include_path’, ‘C:/wamp/bin/php//mysql.php/’ . PATH_SEPARATOR . ini_get(‘include_path’));[/php]

or
[php]ini_set(‘C:/wamp/bin/php//mysql.php’, ‘/path/to/mysql.php/’ . PATH_SEPARATOR . ini_get(‘C:/wamp/bin/php//mysql.php’));[/php]

If mysql.php was in C:/wamp/bin/php/ then it would be:

[php]ini_set(‘include_path’, ‘C:/wamp/bin/php/’ . PATH_SEPARATOR . ini_get(‘include_path’));[/php]

It should be in a directory like C:/wamp/bin/php/pear/DB/

Ok I started all over and reinstalled WAMP, and the files, but now with the new code I am still not getting it to connect to mysql.php

[php]<?php
ini_set(‘include_path’, ‘C:\wamp\bin\php\php5.3.5\PEAR\DB\mysql.php’ . PATH_SEPARATOR . ini_get(‘include_path’));

// load the PHP PEAR database library
include(‘DB.php’);

// connect to the database, providing the DB user-ID, password, host name, DB
$connection = ‘mysql://root:@localhost/infs734’;
$database = DB::connect($connection);

if (DB::isError($database)) {
die($database->getMessage() . ‘
’ . $database->getDebugInfo());
}
?>[/php]

instead of:

[php]ini_set(‘include_path’, ‘C:\wamp\bin\php\php5.3.5\PEAR\DB\mysql.php’ . PATH_SEPARATOR . ini_get(‘include_path’));[/php]

I think you want:

[php]ini_set(‘include_path’, ‘C:\wamp\bin\php\php5.3.5\PEAR\DB’ . PATH_SEPARATOR . ini_get(‘include_path’));[/php]

Are you getting any error messages?

Parse error: syntax error, unexpected T_STRING in C:\wamp\www\dbconnect.php on line 2

Can you post the contents of dbconnect.php

[php]<?php
ini_set(‘include_path’, ‘C:\wamp\bin\php\php5.3.5\PEAR\DB’ . PATH_SEPARATOR . ini_get(‘include_path’));

// load the PHP PEAR database library
include(‘DB.php’);

// connect to the database, providing the DB user-ID, password, host name, DB
$connection = ‘mysql://root:@localhost/infs734’;
$database = DB::connect($connection);

if (DB::isError($database)) {
die($database->getMessage() . ‘
’ . $database->getDebugInfo());
}
?>[/php]

Whoops missing a \ on this line:

[php]ini_set(‘include_path’, ‘C:\wamp\bin\php\php5.3.5\PEAR\DB’ . PATH_SEPARATOR . ini_get(‘include_path’)
[/php]

replace with:

[php]ini_set(‘include_path’, ‘C:\wamp\bin\php\php5.3.5\PEAR\DB\’ . PATH_SEPARATOR . ini_get(‘include_path’));[/php]

Now its back to the Unable to include the DB/mysql.php file for ‘mysql://root:@localhost/infs734’. Is there a way to take how the database is called out and still have it work another way?

Ok chances some stuff up and used a different way to connect.
Now there is a query error
Fatal error: Call to a member function query() on a non-object in C:\wamp\www\listcontacts.php on line 36

[php]<?php
$query = "SELECT * FROM contacts ORDER BY ".$_SESSION[‘sort’];
$result = $database->query($query);
for ($i=0; $i<$result->numRows(); $i++) {
$contact = $result->fetchRow(DB_FETCHMODE_ASSOC,$i);
?>[/php]

[php] $result = $database->query($query);[/php]

How are you connecting?

[php]<?php
$link = mysql_connect(‘localhost’, ‘root’, ‘’);
if (!$link) {
die('Could not connect: ’ . mysql_error());
}
echo ‘’;
mysql_close($link);
?>[/php]

You need to use mysql_query() instead of $database->query()

Something like:
[php]
$query = "SELECT * FROM contacts ORDER BY " . mysql_real_escape_string($_SESSION[‘sort’]);
$result = mysql_query($query);

while ($contact = mysql_fetch_assoc($result)) {
//rest of code here
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service