开发者

PHP 5.3.5-1 connect to MySQL DB on localhost, Ubuntu 11.04

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 prob开发者_StackOverflow中文版lem 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');


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.:)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜