Binary Expressions

« Previous Page

2005-8-9

PHP/MySQL Query Example

Filed under: — The Warden @ 3:25 pm

A basic MySQL query using PHP with commenting marked with //.

// SQL statement that will be sent to the MySQL database server.
$szQry = “SELECT column1, column2 FROM foo”;

// MySQL database login credentilas; host (127.0.0.1), username and password.
$szDBConn = mysql_connect(”host”,”username”,”password”);

// Select database using the login credentials provided above.
mysql_select_db(”database_name”, $szDBConn);

// Send SQL statement to database.
$saResults = mysql_query($szQry, $szDBConn);

// Fetch results retured back from the SQL statement.
$obResults = mysql_fetch_row($saResults);

MySQL Lost connection to MySQL server during query

Filed under: — The Warden @ 12:41 pm

For more than 3 hours I’ve been trying to find a solution to the error I kept receiving when doing a basic query to MySQL through PHP. It kept returning back to me “Lost connection to MySQL server during query”. Right away I went to my PHP, MySQL and Apache logs to find more information. I came back with nothing more than the error message I already received. So as I usually do off I went to Google and IRC for some help. Neither were leading into any direction to a solution. Even after finding the MySQL documentation talking about the “Lost Connection” or “server has gone away” error I didn’t find a solution. I started talking to dhartmei on EFNet in the #OpenBSD channel for some assistance. He stepped me through doing the following at the command prompt.

# nc -v 127.0.0.1 3306
Connection to 127.0.0.1 3306 port [tcp/mysql] succeeded!
Host ‘localhost.domain.com’ is not allowed to connect to this MySQL

# netstat -an | grep 3306
tcp 0 0 *.3306 *.* LISTEN

# mysql -h 127.0.0.1 -u username -p database
Enter password:
ERROR 2013: Lost connection to MySQL server during query

This determined that port 3306 was listening for connections however localhost/127.0.0.1 returned an error message of “Host ‘localhost’ is not allowed to connect to this MySQL”. If you haven’t caught on already this means I forgot to set MySQL permissions correctly for the user. Remember that localhost and 127.0.0.1 is different according to MySQL permissions.

A quick GRANT (i.e. grant all ON database.table TO username@localhost identified by ‘password-here’) command in MySQL resolved the problem.

« Previous Page

Take back your mailbox - CAUCE.org

Powered By Wordpress PHP: Hypertext Preprocessor MySQL Powered Download Juice, the cross-platform podcast receiver
Proud To Be Canadian Get Firefox Valid XHTML Valid CSS
<NO>OOXML Logo


25 queries. 0.316 seconds.
Copyright © 2004 - 2005 by Adam Douglas