简体   繁体   English

无法连接到本地mysql服务器

[英]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. 尝试连接到我的mysql数据库时出现错误,我是一个PHP / mysql业余爱好者,所以希望你们能帮到我,因为我被卡住了。

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? 我感觉问题不在于脚本本身,而在于mysql服务器中的某个地方? 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. 我认为您的问题可能是您正在使用mysqli_connect连接到服务器,但是您正在尝试使用mysql_query运行查询。

Try using mysqli to run the query. 尝试使用mysqli运行查询。

Here is the manual for mysqli MYSQLi Manual 这是mysqli的手册MYSQLi手册

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 Net Tuts-PHP准备语句的问题

You can either change the way you connect to you server, using mysql like so: mysql_connect(servername,username,password); 您可以使用mysql这样更改与服务器的连接方式: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: 首先,您需要获取连接,所以可以说您将其存储在$ mysqli中,然后我们可以运行查询:

$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. 您正在连接到mysqli_connect并在mysql_query运行查询,这就是为什么它未建立连接。

Assuming you are including the file as well in correct manner ;) 假设您也以正确的方式包含文件;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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