php mysql查询连接错误无法选择数据库

时间:2021-09-22 23:07:25

I'm having some trouble making a connection to my database. As far as I'm concerned I'm using the same code as usual. But suddenly I can't seem to make a connection. I've tried the script locally using MAMP aswell as online on my server.

我的数据库连接有问题。就我而言,我使用的代码和往常一样。但突然之间,我似乎无法建立起联系。我已经在本地使用MAMP和服务器上的在线脚本进行了尝试。

I use a config file to select and connect the database with the following code:

我使用配置文件选择并连接数据库,代码如下:

<?php
  $db_host = "localhost";
  $db_username = "root";
  $db_password = "root";
  $db_database = "dbname";
$link = mysqli_connect($db_host,$db_username,$db_password) or die("Cannot connect");
  mysqli_select_db($link, $db_database) or die("Cannot select database");
  ?>

Next I try to print info from my database on my page using the following code:

接下来,我尝试从我的数据库打印信息在我的页面使用以下代码:

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

I've been staring my eyes out and don't know how to move on. Keep coming up with the same problem. If someone could help me out, it would be so much appreciated.

我一直盯着外面看,不知道该怎么走。继续提出同样的问题。如果有人能帮助我,我将不胜感激。

That's the error message:

这是错误信息:

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 11

mysql_query()函数。:无法通过套接字'/var/run/mysqld/mysqld连接到本地MySQL服务器。在第11行,在/public/sites/www.kernlab.nl/website/index.php中

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 12 Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

警告:mysql_query()函数。[MySQL -query]:在/public/sites/www.kernlab.nl/website/index.php行12上无法通过socket '/var/run/mysqld/mysqld连接到本地MySQL服务器。袜子”(2)

1 个解决方案

#1


3  

In config you use mysqli_ (mysqli extenton) methods.

在配置中,您使用mysqli_ (mysqli extenton)方法。

Then you use mysql_ (mysql extenton) methods.

然后使用mysql_ (mysql extenton)方法。

You are using different extentions. Use same extention in both cases.

你在使用不同的扩展。在这两种情况下使用相同的扩展。

#1


3  

In config you use mysqli_ (mysqli extenton) methods.

在配置中,您使用mysqli_ (mysqli extenton)方法。

Then you use mysql_ (mysql extenton) methods.

然后使用mysql_ (mysql extenton)方法。

You are using different extentions. Use same extention in both cases.

你在使用不同的扩展。在这两种情况下使用相同的扩展。