1

trying to get my first little PHP script to connect to a local mySql database and am having some trouble. I used a cookie cutter script from the net to get going and am having some issues. In order to debug so far all I've done is place echo statements before and after the line where I connect to the database and noticed that there is a problem with the connection statement.

Here is the code i'm using, taken verbatim from W3Schools:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

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

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

mysql_close($con);
?>

Naturally I changed the passwords and all that and verified that I can connect to the database from the mysql command line tool.

Any tips would be greatly appreciated.

Here is the log located at: /var/log/mysql/error.log

Version: '5.1.54-1ubuntu4'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)
110727  1:38:22 [Note] /usr/sbin/mysqld: Normal shutdown

110727  1:38:22 [Note] Event Scheduler: Purging the queue. 0 events
110727  1:38:23  InnoDB: Starting shutdown...
110727  1:38:25  InnoDB: Shutdown completed; log sequence number 0 15895194
110727  1:38:25 [Note] /usr/sbin/mysqld: Shutdown complete

110727 14:08:56 [Note] Plugin 'FEDERATED' is disabled.
110727 14:08:56  InnoDB: Initializing buffer pool, size = 8.0M
110727 14:08:56  InnoDB: Completed initialization of buffer pool
110727 14:08:57  InnoDB: Started; log sequence number 0 15895194
110727 14:08:58 [Note] Event Scheduler: Loaded 0 events
110727 14:08:58 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.1.54-1ubuntu4'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  (Ubuntu)

SO I learn't how to get some meaningful debug info from PHP thanks to @nix, add the following lines to the top of the php file:

ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
6
  • 1
    What are the error messages you're getting? Commented Jul 27, 2011 at 19:05
  • No error message is displayed unfortunately... how can I force PHP to show me errors? Commented Jul 27, 2011 at 19:21
  • How did you notice there was a problem with the connection statement then? Commented Jul 27, 2011 at 19:23
  • by throwing in an echo statement before this line: $con = mysql_connect("localhost","peter","abc123"); and after that line I observed that only one of the outputs made it, thus the script must have been interrupted at that line. Commented Jul 27, 2011 at 19:32
  • try sudo service mysqld restart Commented Jul 27, 2011 at 19:41

2 Answers 2

1

Well. try restarting mysql daemon:

sudo service mysqld restart

try connecting to mysql from command line using

-h localhost

and

-h 127.0.0.1

Also - check for exact error you're getting in:

/var/log/mysql.err

or

/var/log/mysql/mysql.log

P.S. your code works for me.

UPDATE: So - in the end as it turns out - mysqli was disabled in PHP. Problem solved.:)

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

8 Comments

as usual: this is just an advice on troubleshooting steps. OP clearly states: Any tips would be greatly appreciated. Downvoter - please explain your logic.
+1; if there is an answer to this question then it looks something like this.
Restarted the service, same unfortunate result.
Try posting the content of your /etc/mysql/my.cnf file on pastie.org so we can take a look
bind-address = 127.0.0.1 - means that mysql listens on 127.0.0.1 only. You have to use that in your mysql script or comment this line out. Don't forget to restart mysqld after any my.cnf changes. There! solved :)
|
0

So it turns out that the php5-mysql library was not installed.

sudo apt-get install php5-mysql

that's it!

Thanks to @nix for taking care of this one.

1 Comment

If you never used PHP before; create php file with this content: <?php echo phpinfo();?> When you open it in the browser - you'll see a lot of information about your PHP install. You'll be able to easily determine if required extensions are installed.

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.