php 如何检查mysql select语句结果在php中是否有值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2793837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:41:35  来源:igfitidea点击:

How to check whether a mysql select statement result has a value or not in php?

phpmysqlselectif-statement

提问by udaya

I have a textbox UserNameand a Check Availabilitybutton next to it..... I have checked the availability by passing UserNametextbox value..... But it doesn't seem to work....

我有一个文本框UserNameCheck Availability旁边的按钮......我已经通过传递UserName文本框值检查了可用性......但它似乎不起作用......

Here is what i am doing?

这是我在做什么?

echo $UserName = $_GET['CheckUsername'];
$_SESSION['state'] = $State;
$queryres = "SELECT dUser_name FROM tbl_login WHERE dUser_name='$UserName'";
$result = mysql_query($queryres,$cn) or die("Selection Query Failed !!!");

if($result==true) // this condition doesn't seem to work
    {
       echo "User Name Available";
    }
else
{
        echo "Sorry user name taken";
}

回答by awgy

Please make sure you're escaping your inputs for MySQL. Passing data directly from $_GET, $_POSTor any of the other superglobals is unsafe.

请确保您正在转义 MySQL 的输入。直接从传递数据$_GET$_POST或任何其它超全局是不安全的。

// Escape any quote characters in the input
$UserName = mysql_real_escape_string($_GET['CheckUsername'], $cn);
$_SESSION['state'] = $State;

$queryres = "SELECT dUser_name FROM tbl_login WHERE dUser_name='$UserName' LIMIT 1";
$result = mysql_query($queryres, $cn) or die("Selection Query Failed !!!");

if (mysql_num_rows($result) > 0) {
    echo 'User name exists in the table.';
} else {
    echo 'User name does not exist in the table.';
}

回答by Gazler

if ($result == false)
{
      echo "not available";
}
else
{
      echo "available";
}

The reason yours doesn't work is because the value of $result will not be true as it contains an array. You could do it by reversing the statement to check for false first.

你的不起作用的原因是因为 $result 的值不会为真,因为它包含一个数组。您可以通过反转语句以首先检查 false 来实现。

You can also do it with mysql_num_rows

你也可以用 mysql_num_rows

if (mysql_num_rows($queryres) > 0)
{
     echo "User name taken";
}

回答by AllenJB

from http://php.net/mysql_query:

来自http://php.net/mysql_query

Return Values

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

返回值

对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 在成功时返回资源,在错误时返回 FALSE。

So to start off with, your if statement will never evaluate to false, because you have the "or die" on the mysql_query (which is activate when mysql_query returns false).

因此,首先,您的 if 语句永远不会评估为 false,因为您在 mysql_query 上有“or die”(当 mysql_query 返回 false 时激活)。

But your if condition is wrong altogether, because you don't want to know if the query failed, but if any results were returned. For this you probably want to use mysql_num_rows.

但是您的 if 条件完全错误,因为您不想知道查询是否失败,但是否返回了任何结果。为此,您可能想要使用 mysql_num_rows。

As a small tip, since you only need to know if there's 1 matching username, add "LIMIT 1" to the end of your query. Then mysql will return as soon as it hits the first match, instead of searching th whole table for more results.

作为一个小提示,由于您只需要知道是否有 1 个匹配的用户名,请在查询末尾添加“LIMIT 1”。然后 mysql 会在遇到第一个匹配项时立即返回,而不是在整个表中搜索更多结果。

As an alternative method, you could use a COUNT() query, and check the number returned in the result set.

作为替代方法,您可以使用 COUNT() 查询,并检查结果集中返回的数字。

回答by walkthroughthecloud

Although this question has already an adequate answer, I'll give you another option. Simply remove the "==true" in the IF condition and it will automatically test for the presence of a value instead of a boolean comparison.

虽然这个问题已经有了足够的答案,我再给你一个选择。只需删除 IF 条件中的“==true”,它就会自动测试值的存在,而不是布尔比较。

if($result) // this condition will now work
    {
       echo "User Name Available";
    }
else
{
        echo "Sorry user name taken";
}

You can use this if you want to preserve the signature of your code.

如果您想保留代码的签名,可以使用它。

回答by Saraf Sissddharth

After Executing the Query just mysql_num_rows($result) which will return you the Count of Rows is fetched.

执行查询后, mysql_num_rows($result) 将返回您获取的行数。

回答by Ranjith

$queryres = "SELECT dUser_name FROM tbl_login WHERE dUser_name='$UserName'";
$result = mysql_query($queryres,$cn) or die("Selection Query Failed !!!");
if (mysql_num_rows($result))
{
echo "Username available";
}
else
{
echo "Username not available";
}