简体   繁体   English

基本的MySQL查询没有结果

[英]Basic mysql query giving no results

I got a simple query looking like this: 我得到一个简单的查询,看起来像这样:

$con = mysqli_connect("host","username","password","default_database");

if(mysqli_connect_errno($con))
{
    mysqli_connect_error();
}
else
{
    $query = "SELECT * FROM `users_t`";
    $result = mysqli_query($query) or die (mysql_error());
    while($row = mysqli_fetch_assoc($result))
    {
        echo $row['email'];
    }

}

Running this query gives me absolutely nothing. 运行该查询绝对没有给我任何帮助。 No errors but no result at the same time. 没有错误,但同时没有结果。 I can't figure out whats wrong, help me please. 我不知道怎么了,请帮帮我。

It looks like there may be a few things going on here: 看来这里可能发生了一些事情:

  1. According to the mysqli_connect_errno() Documentation , you should be checking for !$con rather than mysqli_connect_errno($con) in your if statement. 根据mysqli_connect_errno()文档 ,您应该在if语句中检查!$con而不是mysqli_connect_errno($con)
  2. When a connection error is encountered, you're calling the error function but not printing it. 遇到连接错误时,您正在调用错误函数,但不打印它。
  3. According to the mysqli_query() documentation , the first argument should be the database connection, the second being the query itself. 根据mysqli_query()文档 ,第一个参数应该是数据库连接,第二个参数是查询本身。
  4. When a query errors out, you're calling mysql_error() when you should be calling mysqli_error() , passing it the connection. 当查询出错时,您应在调用mysqli_error()时调用mysql_error()mysqli_error()其传递连接。 Again, according to documentation 同样,根据文档

Try this out and see if this resolves your problems: 尝试一下,看看是否可以解决您的问题:

<?php
$con = mysqli_connect("host","username","password","default_database");

if(!$con) {
  print mysqli_connect_error();
} else {
  $query = "SELECT * FROM `users_t`";
  $result = mysqli_query($con, $query) or die (mysqli_error($con));
  while($row = mysqli_fetch_assoc($result)) {
    echo $row['email'];
  }
}
$result = mysqli_query($query) or die (mysql_error());

Needs a mysqli resource link and also its not mysql_error() 需要一个mysqli资源链接,也不需要mysql_error()

if ($result = mysqli_query($con,$query))
{
while($row = mysqli_fetch_assoc($result))
    {
        echo $row['email'];
    } 
}

PHP is case-sensitive, so there is a very good change that your column in the database is actually named Email and then you have no result when you check $row['email'] , because the E has to be upper case. PHP是区分大小写的,因此有一个很好的变化,即数据库中的列实际上命名为Email ,然后在检查$row['email']时没有结果,因为E必须为大写。

What you can do is make sure you use the right case in PHP, or select just the fields you want. 您可以做的是确保在PHP中使用正确的大小写,或仅选择所需的字段。 MYSQL isn't case-senstive, so if you do the following it will work. MYSQL不区分大小写,因此,如果执行以下操作,它将起作用。

Also a good way to check if you have the right case: use var_dump($row) (I've added it to the code just to show you) 这也是检查您是否具有正确大小写的一种好方法:使用var_dump($row) (我已将其添加到代码中只是为了向您展示)

$con = mysqli_connect("host","username","password","default_database");

if(mysqli_connect_errno($con))
{
    mysqli_connect_error();
}
else
{
    $query = "SELECT email FROM `users_t`";
    $result = mysqli_query($query) or die (mysql_error());
    while($row = mysqli_fetch_assoc($result))
    {
        echo $row['email'];
        var_dump($row);
    }

}

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

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