0

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);
5
  • Make sure you have error_reporting(E_ALL); and ini_set('display_errors', 1); If the MySQL extension isn't loaded on the Mac, it will error, but not die() Commented Mar 20, 2012 at 17:06
  • what does mysql_num_rows($result); give you? Commented Mar 20, 2012 at 17:07
  • Try using mysql_fetch_assoc instead of mysql_fetch_array, and test1, from $row['test1'] must actually be a column name. Commented Mar 20, 2012 at 17:08
  • Also make sure the database user has permission on the Mac (though if it's root that shouldn't be an issue) Commented Mar 20, 2012 at 17:08
  • @SorinButurugeanu not really needed as by default mysql_fetch_array returns associate and numeric array Commented Mar 20, 2012 at 17:11

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

0

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());

Comments

0

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

4 Comments

There is no difference between how Windows, Linux and OSX treat case sensitivity in regards to tables. myTable, MYTABLE or mYtAbLe will all work, regardless of operating system.
@KristianAntonsen that's not true, i've had that problem a lot of times... dev.mysql.com/doc/refman/5.0/en/…
You're right, there is a difference with Linux, but none with Windows or OSX which is what your answer claims.
@KristanAntonsen Mac Os can be case sensitive. reading the docs: However, Mac OS X also supports UFS volumes, which are case sensitive just as on any Unix.
0

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();

        }

1 Comment

Apropos execption: I think an exception should be thrown by the db driver, not by your code, because the main advantage of exceptions is that you don't have to check the return code on every step. PDO does this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.