简体   繁体   English

PHP无法打印mysql_query()结果

[英]PHP unable to print mysql_query() result

I have code 我有代码

$email = "jb@tlb.com";    
$row = mysql_query("SELECT EXISTS(SELECT email FROM accounts WHERE email='".$email."');");
echo $row[0];

However, nothing is echoed. 但是,没有任何回应。

This is strange because something is being returned because, later in the code I have a function that is CALLED: 这很奇怪,因为正在返回某些内容,因为在代码的后面,我有一个被调用的函数:

if ( $row[0] == 0 ) { echo "Email address is available<br>";};

However: this is strange because when i put the SAME CODE into mySQL database command prompt: 但是:这很奇怪,因为当我将SAME CODE放入mySQL数据库命令提示符时:

在此处输入图片说明

It clearly returns 1 or TRUE. 它显然返回1或TRUE。

The mysql_query is returning 0 when the same exact command in mysql command prompt returns 1. Further: I am unable to echo the result for debugging purposes. 当mysql命令提示符中的相同精确命令返回1时,mysql_query返回0。此外:我无法回显结果以进行调试。

EDIT: Please not, the regular mySQL command is returning this and ONLY this: 编辑:请不要,常规的mySQL命令将返回此并且仅此: 在此处输入图片说明

EDIT: Here is there entire database: 编辑:这里有整个数据库: 在此处输入图片说明

MySQL query gives you a ressource. MySQL查询为您提供了资源。 After that you have to fetch the data with mysql_fetch_assoc or mysql_fetch_row or something else for example. 之后,您必须使用mysql_fetch_assoc或mysql_fetch_row或其他方式获取数据。 But its better to use prepared statements with mysqli or PDO to get more security. 但是最好将准备的语句与mysqliPDO结合使用,以获得更高的安全性。

$email = "jb@tlb.com";    
$res = mysql_query("SELECT EXISTS(SELECT email FROM accounts WHERE email='".myql_real_escape_string($email)."')");
$row = mysql_fetch_assoc($res);
echo $row['email'];

Answer to your question: 回答您的问题:

$email = "jb@tlb.com";    
$res = mysql_query("SELECT email FROM accounts WHERE email='".mysql_real_escape_string($email)."')");
$numRows = mysql_num_rows($res);

if($rowRows > 0) {
    echo "Record Available";
}

You need to actually retrieve the result set from the query. 您实际上需要从查询中检索结果集。 mysql_query() just returns a resource handle for a successful select. mysql_query()仅返回成功选择的资源句柄。 You then need to fetch the results using mysql_fetch_* class of functions. 然后,您需要使用mysql_fetch_*函数类来获取结果。 Alternatively, you can use mysql_num_rows() to determine the number of rows returned in the result set. 另外,您可以使用mysql_num_rows()确定结果集中返回的行数。

In this case it is really senseless to wrap your actual query into a subquery. 在这种情况下,将实际查询包装到子查询中实际上是没有意义的。 Just run your select and determine the number of rows: 只需运行您的select并确定行数:

$email = "jb@tlb.com";    
$result = mysql_query("SELECT email FROM accounts WHERE email='".$email . "'");
if($result) {
    $row_count = mysql_num_rows($result);
    echo $row_count;
}

Also, you should not be writing new code using mysql_* functions, as these are deprecated. 另外,您不应该使用mysql_*函数编写新代码,因为这些函数已被弃用。 I would suggest mysqli or PDO extensions instead. 我建议使用mysqliPDO扩展。

You need to do something like 你需要做类似的事情

while ($r = mysql_fetch_assoc($row)) 
{ 
    echo $r[0];                                 
}

after that code. 该代码之后。

Let me know. 让我知道。

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

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