[英]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: 看来这里可能发生了一些事情:
!$con
rather than mysqli_connect_errno($con)
in your if
statement. if
语句中检查!$con
而不是mysqli_connect_errno($con)
。 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.