简体   繁体   中英

Can't connect to MySql using PHP

I have the same issues described here: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

but, I am certain my credentials are in correctly.

I am using a hosting company and ( tsohosts.co.uk ) and assume that MySql starts automatically.

I'm not sure what to check next. Is there any way to get more details about the "or die" phrase.

Here is my code:

$dbc = mysqli_connect("localhost","CorrectUser","CorrectPass",CorrectDB") 
or die ('No can do');

$query = "INSERT INTO users(firstName,lastName,username,password,email,dateJoin,school,level) " 
." VALUES ('value','value','value','value','value', NOW(),'value','Not yet set')";

mysqli_query($dbc, $query) or die ("I couldn't get it in!"); 

mysqli_close($dbc);
?>

and the error is

Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in ../testdb.php on line 16
No can do

Do I have to do something else start MySQL within the PHP script?

You missed a double quote in $dbc try this

$dbc = mysqli_connect("localhost","CorrectUser","CorrectPass","CorrectDB") 
or die ('No can do');

You put a double quote and a fullstop at the wrong place in $query it should be like this

$query = "INSERT INTO users(firstName,lastName,username,password,email,dateJoin,school,level)
 VALUES ('value','value','value','value','value', NOW(),'value','Not yet set')";

Also remove $dbc from the mysqli_query() function and also escape the single quote in the die() function like so:

mysqli_query($query) or die ("I couldn't get it in!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM