Jump to content

Recommended Posts

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?

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/153222-wont-connect-to-database/
Share on other sites

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?

 

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?

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.