Lodius2000 Posted April 20, 2008 Share Posted April 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/ Share on other sites More sharing options...
DarkWater Posted April 20, 2008 Share Posted April 20, 2008 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). =) Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522269 Share on other sites More sharing options...
Lodius2000 Posted April 20, 2008 Author Share Posted April 20, 2008 this prints nothing also, about the if(count), just copied that from the book, but will do Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522301 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522324 Share on other sites More sharing options...
Lodius2000 Posted April 21, 2008 Author Share Posted April 21, 2008 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. this still prints nothing at all Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522358 Share on other sites More sharing options...
Lodius2000 Posted April 21, 2008 Author Share Posted April 21, 2008 so if there is something wrong with my dsn what could it be? Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522361 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 That's very odd. Just put this line right after the <?php line. echo "Testing output."; Make sure that output works at all on that script. Very strange though. >_> Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522363 Share on other sites More sharing options...
Lodius2000 Posted April 21, 2008 Author Share Posted April 21, 2008 that works just fine, prints "Testing output." Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522370 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522372 Share on other sites More sharing options...
Lodius2000 Posted April 21, 2008 Author Share Posted April 21, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522386 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522399 Share on other sites More sharing options...
Lodius2000 Posted April 21, 2008 Author Share Posted April 21, 2008 i think it can get them from the web, w3schools to be specific, i just wonder why my book had me go through all teh pear stuff, it didnt even mention that php is capable of connecting to mysql by itself, though i was sure it was Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522405 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 Just a big tip: The PHP manual is your friend. To check any function's syntax, go to http://www.php.net/FUNCTION_NAME and it'll give you the function's page. I can teach you how to use the MySQL functions if you want rather than teaching yourself. Just tell me if you need help, lol. Quote Link to comment https://forums.phpfreaks.com/topic/102049-first-time-trying-to-connect-to-database/#findComment-522410 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.