Meowbits
Meowbits

Reputation: 586

Database connection problems in PHP/MySQL

When I run the script below with the added line,

$count = 1;

I get a value of 1 on the screen, but when I take that line out I don't get get anything at all. tried moving it above the $count=mysql_num_rows($result); and I still didn't get a value.

$sql="SELECT EMAIL FROM CUSTOMER WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

$count = 1;
echo $count;

What am I doing wrong here? I have never used PHP until now. The error is:

Can't connect to local MySQL server through socket
'/var/run/mysqld/mysqld.sock' (2)

Upvotes: 2

Views: 1697

Answers (3)

Ehsan Khodarahmi
Ehsan Khodarahmi

Reputation: 4932

When you can't connect successfully to the database, return value from mysql_query function ($result) would not be a valid value. so when you give it to mysql_num_rows function, it fails & returns FALSE value, which has no visual effect on output screen!

Upvotes: 0

n00dle
n00dle

Reputation: 6053

I would guess at an a error with your SQL query (var_dump($count); would return false in this case).

To check this, I would do 2 things:

  • After your query do if(!$result) echo mysql_error( ); - this will show you any errors that happen while talking to the database
  • To check your SQL is being formed correctly, create it in a variable (e.g. $sql = "SELECT email ...";) and echo it out.

EDIT: Ahh just seen the update - it's a connection issue. Check that your mysql_connect( ) has the right host, username & password. Otherwise it could be a problem on your system (e.g. firewall or similar)

EDIT 2: As has been rightly pointed out, mysql_connect details would cause a different connection error to the one you're seeing. I've had a quick Google for it, and this cropped up http://lists.mysql.com/mysql/204035. Not sure it'll be any use as I've not read it through, but it does describe some steps for the solving this problem on someone else's system.

Upvotes: 0

Anonymous
Anonymous

Reputation: 553

This means that your MySQL server socket (/var/run/mysqld/mysqld.sock) is either missing or corrupt.

It could also mean that MySQL service is not working right, try restarting in SSH using:

$ service mysqld restart

If it says the service is missing, then say:

$ service mysql restart

Upvotes: 1

Related Questions