Jump to content

first time trying to connect to database


Lodius2000

Recommended Posts

ok so im working through 'learning php 5' by o'reilly, so if you have it this will probably help, chapter 7 exercise 1. I have to connect to my database using peardb, I am doing this live on the web, just for easy's sake because i work on several different computers. My free hosting service help ticket system keeps giving me the run around on how to fix this so I would like your help.

 

the hosting service uses cpanel so in additon to your username that you create for your database you need to have your cpanel id in there to, making the username look like this a2378756_username, same thing for the database. also the faqs say to use localhost as the DSN

 

here are some relevant faqs from the hosting service in case there is anything you think I am doing wrong

http://www.000webhost.com/?page=faq&ID=20  ---about using localhost

http://www.000webhost.com/?page=faq&ID=46  ---about using cpanel id

 

also I have created the necessary table and values of said table using PHPmyadmin so i know they are there

 

lastly I have changed the logins and passwords to protect myself but they are correct for sure in the script

 

so here it is

 

<?php

require '../../php/DB.php';

$db = DB::connect('mysql://a1234567_username:password@localhost/a1234567_servername');

if (DB::isError($db)) {

die("Can't connect: " . $db->getMessage());

}

 

$db->setErrorHandling(PEAR_ERROR_DIE);

$db->setFetchMode(DB_FETCHMODE_ASSOC);

 

$dishes = $db->getALL ('SELECT dish_name, price FROM dishes ORDER BY price');

if ( count($dishes) > 0){

print "<ul>";

foreach($dishes as $dish){

print "<li>$dish[dish_name], $dish[price]</li>";

}

print "</ul>";

} else {

print "No data to display";

}

 

 

?>

 

so the problem is that this prints nothing, no ul not error no nothing so I dont know what its doing

 

help

 

Thanks

Link to comment
Share on other sites

Comment out the whole thing with the dishes (after the connection) and do this:

 

$selectnow = $db->getALL("SELECT NOW() as now");
if (!$selectnow) {
  echo "Something's wrong";
}
else {
  echo $selectnow['now'];

}

 

See if that returns anything.  I think the problem is with your query.  Oh, and by the way, don't do if (count($dishes)), do if is_array($dishes). =)

 

Link to comment
Share on other sites

this prints nothing also, about the if(count), just copied that from the book, but will do

 

Do this right after the DB::Connect line.

if (PEAR::isError($db)) {
     die($db->getMessage());
}

 

Tell me if that kills the script with an error message.  Your DSN might be incorrect if that's the case. :D

Link to comment
Share on other sites

 

Do this right after the DB::Connect line.

if (PEAR::isError($db)) {
     die($db->getMessage());
}

 

Tell me if that kills the script with an error message.  Your DSN might be incorrect if that's the case. :D

 

this still prints nothing at all

Link to comment
Share on other sites

that works just fine, prints "Testing output."

 

You know the DB::isError($db) line that you have with that if statement?  Try putting that AFTER you set the error handler for the PEAR classes.  That could be why you get no error.  Try that and tell me what it does then.

Link to comment
Share on other sites

still nothing

 

my book says to use the DB::isError($db) line after the connection request because peardb cant automatically set up automatic error handling which is what the $db->setErrorHandling(PEAR_ERROR_DIE); line does, after the connection is set up you can set the automatic error handling but you cant do it as the connection is set up

 

 

EDIT EDIT EDIT

 

Can I ask, how do you connect to your server, what pear module do you use (if you use one), what does your coding look like, do you think i could adapt it to my system?

Link to comment
Share on other sites

If you can't get the PEAR to work for whatever reason, I'd just use the MySQL specific functions.  Do you see yourself changing databases or releasing your code for others using other DBs to use? =/  I doubt it, so you can use the MySQL functions.  I can help if you don't know how to use those.

Link to comment
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.