简体   繁体   中英

Can't connect to local mysql server

I get an error trying to connect to my mysql database, I'm quite a PHP/mysql amateur so I hope you guys can help me out since I'm stuck.

Warning: mysql_query() [function.mysql-query]: 
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'\
(2) in /public/sites/www.kernlab.nl/website/index.php on line 13

Warning: mysql_query() [function.mysql-query]: A link to the server could not be
established in /public/sites/www.kernlab.nl/website/index.php on line 13
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Below is how I try to connect to my database:

<?php
$db_host = "host";
$db_username = "user";
$db_password = "pass";
$db_database = "db";
$link = mysqli_connect($db_host,$db_username,$db_password) or die("Cannot connect");
  mysqli_select_db($link, $db_database) or die("Cannot select database");
?>

This how I try to echo the data:

<?php
$query="SELECT * FROM ditdoenweblokken ORDER BY id DESC";
$result = mysql_query($query) or die(mysql_error()) ;
while($rij = mysql_fetch_array($result)){  
?>
<li><a style="height:150px;width:150px;" class="fancybox" href="#inline<?php echo($rij['id']);?>">
    <p style="font-weight:bold;"><?php echo($rij['titel']);?></p>
    <?php echo($rij['quote']);?></a>
</li>
<?php
}
?>

I get the feeling the problem is not in the script itself but somewhere in the mysql server? Hope you guys can help since I'm stuck.

Thanks in advance

I think your problem might be that you are connecting to the server using mysqli_connect but you are trying to run a query using mysql_query.

Try using mysqli to run the query.

Here is the manual for mysqli MYSQLi Manual

On a side note, this might help when thinking about how you get your data from your database and the security of it:

Net Tuts - The Problem with PHP's Prepared Statements

You can either change the way you connect to you server, using mysql like so: mysql_connect(servername,username,password);

However this is not secure. Or you can change the way you are doing the query as stated before.

First you need to get the connection so lets say you store it in $mysqli Then we can run the query:

$result = $mysqli->query("SELECT * FROM ditdoenweblokken ORDER BY id DESC);

Now you can check to see if anything has been returned from the query.

You are connecting to mysqli_connect and running queries in mysql_query , thats why its not establishing connection.

Assuming you are including the file as well in correct manner ;)

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