NullPoiиteя
NullPoiиteя

Reputation: 57312

PHP not displaying MYSQL result

I created a script on a windows platform which connects to the mysql database and returns the results of a table. A very basic script which I wrote to simply test my connection worked. The script works fine on my windows machine but not on my new mac. On the mac it simply does not display any records at all.

I know that the database connection has been established because there is no error but I can not see why the result set is not being displayed on screen, as I said it worked fine on my windows machine.

The Mac has mysql (with data) and apache running for php.

Please could someone help as I have no idea what to do now?

Script below:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'test';

mysql_select_db($dbname);

mysql_select_db("test", $conn);

$result = mysql_query("SELECT * FROM new_table");

while($row = mysql_fetch_array($result))
  {
  echo $row['test1'] . " " . $row['test2'] . " " . $row['test3'];
  echo "<br />";
  }

mysql_close($con);

Upvotes: 0

Views: 7538

Answers (5)

kba
kba

Reputation: 19466

There are various things you could do to debug this.

  • Show all PHP errors.

    ini_set('display_errors','On');
    ini_set('error_reporting',E_ALL);
    
  • Catch all possible MySQL errors, not only the ones concerning whether you connected successfully.

    mysql_select_db("test", $conn) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());
    $result = mysql_query("SELECT * FROM new_table") or die(mysql_error());
    

Side note: There's no reason to select which database you wish to use twice.

Upvotes: 1

Baba
Baba

Reputation: 95101

I think you should use the improved PHP mysql driver

        try
        {

            $db = new mysqli("localhost","root","root","test");

            if ($db->connect_errno) {
                throw new Exception($db->connect_error);
            }

            if ($result = $db->query("SELECT * FROM new_table")) {
                printf("Select returned %d rows.\n", $result->num_rows);


                while($row = $result->fetchAssoc())
                  {
                    echo $row['test1'] . " " . $row['test2'] . " " . $row['test3'];
                    echo "<br />";
                  }


                /* free result set */
                $result->close();
            }


            $db->close();   
        }
        catch(Exception $e)
        {
                printf("Database Error: %s\n", $e->getMessage());
                exit();

        }

Upvotes: 0

Manse
Manse

Reputation: 38147

Its very difficult to see what is wrong ... so add some basic error checking, like changing this

$result = mysql_query("SELECT * FROM new_table");

to

$result = mysql_query("SELECT * FROM new_table") or die(mysql_error());

This will show you the error you are getting from your query (if there is one) .. you ill see in the documentation for mysql_query that it returns a boolean if there was an error

Also note that you have a mistake in the variable name for closing the MySQL Connection :

mysql_close($con);

should be

mysql_close($conn);

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76870

The only thing i can think of is that Mac file system is case sensitive while windows isn't so it might be that you mispelled the name of the table. In any cas you should do

$result = mysql_query("SELECT * FROM new_table") or die("error:".mysql_error());

to view the error

Upvotes: 0

Savetheinternet
Savetheinternet

Reputation: 491

Check to see if the SELECT query was successful or not before fetching the rows.

<?php
$result = mysql_query("SELECT * FROM new_table");
if(!$result)
     die('SQL query failed: ' . mysql_error());

Upvotes: 0

Related Questions