cashewpoison Posted April 8, 2009 Share Posted April 8, 2009 I am trying to get information from a MySQL database I set up called askjohnny and to print the contents to an HTML file. Here is my code so far: <head> <title>sql try 1</title> </head> <body> <?php $dbcnx = @mysql_connect('IPAdress', 'UserName', 'Password'); if (!$dbcnx) { exit('<p>Unable to connect to question database server at this time. Error 1.</p>'); } if (!@mysql_select_db('askjohnny')) { exit('<p>Unable to locate questions. Error 2.</p>'); } $result = @mysql_query('SELECT question, answer FROM quest'); if (!$result) { exit('<p>Error performing query: ' . mysql_error() . '</p>'); } while ($row = mysql_fetch_array($result)) { echo '<p>' . 'Question: ' . $row['question'] . ' Answer: ' . $row['answer'] . '</p>'; } ?> </body> I have the ip adress user name and password different in my real thing and that part works, but I am getting the "Unable to locate questions. Error 2". Anyone know why? Quote Link to comment https://forums.phpfreaks.com/topic/153222-wont-connect-to-database/ Share on other sites More sharing options...
Maq Posted April 8, 2009 Share Posted April 8, 2009 Remove all those @ signs and it should output a more descriptive error message. Is 'askjohnny' definitely a database? Quote Link to comment https://forums.phpfreaks.com/topic/153222-wont-connect-to-database/#findComment-804889 Share on other sites More sharing options...
cashewpoison Posted April 8, 2009 Author Share Posted April 8, 2009 yeah, askjohnny is a database, it has some things in it as well Quote Link to comment https://forums.phpfreaks.com/topic/153222-wont-connect-to-database/#findComment-804890 Share on other sites More sharing options...
shlumph Posted April 8, 2009 Share Posted April 8, 2009 Don't suppress errors (using the @ sign). It's a bad habit. Use the "or die" statement instead, as follows: $dbcnx = mysql_connect('host', 'user', 'pass') or die(mysql_error()); mysql_select_db('db_name') or die(mysql_error()); Does the database: askjohnny exist? Are you sure it exists? Quote Link to comment https://forums.phpfreaks.com/topic/153222-wont-connect-to-database/#findComment-804898 Share on other sites More sharing options...
PFMaBiSmAd Posted April 8, 2009 Share Posted April 8, 2009 Put mysql_error() into each of your exit() statements to get php/mysql to tell you why the mysql_ function call failed. You have it in your mysq_query exit() statement, why not in your mysql_connect and mysql_select_db exit() statement? Quote Link to comment https://forums.phpfreaks.com/topic/153222-wont-connect-to-database/#findComment-804900 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.