简体   繁体   English

PHP MySQL数据库连接错误

[英]php mysql database connection error

I have the following php code that gets user login details from a html form: 我有以下php代码,可从html表单获取用户登录详细信息:

  $con=mysqli_connect("host","user","pass","db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "select username from users where user='$_POST[username]' limit1";
$result = mysql_query($query);
echo result;

But when I run it, i seem to be getting these errors: 但是当我运行它时,似乎出现了以下错误:

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/directory omitted' (2) in /directory omitted on line 10 警告:mysql_query()[function.mysql-query]:无法通过第10行/ directory省略的套接字'/ directory included'(2)连接到本地MySQL服务器

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /directory omitted on line 10 警告:mysql_query()[function.mysql-query]:无法在第10行省略的/ directory中建立到服务器的链接

Can anyone please help out? 谁能帮忙吗? thanks very much! 非常感谢!

You have mixed mysqli with mysql so there's a lot of typos 您已经将mysqlimysql混合使用,所以有很多错别字

Code should be: 代码应为:

 $con = mysqli_connect("host","user","pass","db");

 // Check connection
 if (mysqli_connect_errno()) {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 $u = $_POST['username'];

 $sql = "SELECT username FROM users WHERE user='$u' LIMIT 1";
 $query = mysqli_query($con, $sql);
 if ($row = mysqli_fetch_assoc($query)) {
    echo $row['username'];
 }

Hope it worked. 希望它能起作用。

Or if you need all rows printed it should be: 或者,如果您需要打印所有行,则应为:

while ($row = mysqli_fetch_assoc($query)) {
    echo $row['username'];
}
$query = "select username from users where user='$_POST[username]' limit1";
$result = mysql_query($query);

These lines should be Like the following 这些行应如下所示

$query = "SELECT username FROM users WHERE user='".$_POST['username']."' LIMIT 0,1";
$result = mysqli_query($con,$query);
print_r(mysqli_fetch_array($result));

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

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