Error when using mysql_query

Hello,

I am new to php, so I bought a book to teach myself this language. I am trying to create a connection to a database and list the names of the tables it contains. The database has only one table. When I comment out all but the mysql_connect and mysql_select_db statements (and their corresponding echo statements), the code works as expected. However, as soon as I uncomment the mysql_query statement Firefox presents a dialog to open or save the php file. When using Chrome, it shows ‘Error 324 (net::ERR_EMPTY_RESPONSE).’ As I read up on the mysql_query statement, the documentation states that you shouldn’t use a ‘;’ following the show tables command, but the book shows one. Whether I include it or not, though, the problem remains.

Thank you in advance for the assistance.

The code is:

[php]
$con = mysql_connect(“localhost”,“myusername”,“mypassword”);
if (!$con) {
die ("

Error connecting to database: " . mysql_error() . “

”);
}
 echo "<p>Connected to MySQL!</p>";
 
 mysql_select_db("fgexample",$con) or die("<p>Error selecting the database mysql: " . mysql_error() . "</p>");
 
     echo "<p>Connected to MySQL, using database mysql</p>";

     $result = mysql_query("show tables;");

     if (!$result) {
       die("<p>Error in listing tables: " . mysql_error() . "</p>");
     }

     echo "<p>Tables in database: </p>";
     echo "<ul>";
    while ($row = mysql_fetch_row($result)) {
      echo "<li>Table: " . $row[0] . "</li>";
    }
    echo "</ul>";

[/php]

An update to this issue. I installed mamp server to test the script locally and it worked as expected. The server that I am uploading it to is running Debian 6.0.3. I performed a standard install when I installed Debian and MySQL. As I am still new to PHP/MySQL, if this seems to be a MySQL error and I should look to post this is another forum, please let me know. Thank you in advance for any assistance.

On debian, you should have the package php5-mysql installed.

sudo apt-get install php5-mysql mysql-server mysql-client

Will get you all of the packages that should be needed then run:

sudo service httpd restart

(The above depends on you using Apache web server).

Thank you very much for the response. I actually do have those packages installed. The script will connect to the server and I can select the database, but the mysql_query statement seems to cause the script to fail. The server that I am running this on is a old G5 XServe that we had sitting in a closet. If I shouldn’t be having this problem with a standard Debian (+LAMP) install, then I can keep using the local MAMP install to learn on and worry about where our site will be hosted later.

Thanks again.

That might be a good idea. I’ve never heard of something like this happening before and a qwerky error like this could stall your development enough to put you off of it.

Good luck with your learning!

Sponsor our Newsletter | Privacy Policy | Terms of Service