0

mysql 的 PHP Select 语句问题

我得到这个错误..

 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/try/public_html/register.php on line 17

我的代码是

$siteAddress = trim($_POST['b_Address']);

$sql="SELECT * FROM user WHERE siteAddress='$siteAddress';";

$result=mysql_query($sql);
$count=mysql_num_rows($result);

//check for address

if($count)
{
$errorMessage = "<p><font color=red size=4>Site Address " . $siteAddress . " is not available. </font></p>";
$proceed = "no";
}

我尝试 echo $sql ,我得到了这个

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/try/public_html/register.php on line 17
SELECT * FROM user WHERE siteAddress='myshop';

如果我在 phpmyadmin 输入 sql 它会返回一些东西..

     Showing rows 0 - 0 (1 total, Query took 0.0003 sec)
4

3 回答 3

1

你有两个分号

$sql="SELECT * FROM user WHERE siteAddress='$siteAddress';";

它应该是:

$sql="SELECT * FROM user WHERE siteAddress='" . $siteAddress ."'";

你也可以这样做:

$sql= mysql_query("SELECT * FROM user WHERE siteAddress='" . $siteAddress ."'");
$count=mysql_num_rows($sql);
于 2013-10-11T00:27:57.770 回答
1

你可以使用countmysql的功能

$count=mysqli_fetch_assoc(mysqli_query($db,"SELECT count(*) as count FROM user WHERE siteAddress='$siteAddress'"))['count'];

或坏掉

$query=mysqli_query($db,"SELECT count(*) as count FROM user WHERE siteAddress='$siteAddress'");
$result=mysqli_fetch_assoc($query);
$count=$result['count'];

我在示例中使用了 mysqli,因为 mysql 已被弃用,任何访问此页面的人都可能从答案中得到印象,即它仍然可以接受并且可以安全使用。

于 2017-05-22T02:09:04.777 回答
0

尝试这个:

$sql="SELECT * FROM user WHERE siteAddress='{$siteAddress}'";

花括号允许 PHP 将 $siteAddress 变量的内容嵌入到字符串中。另外,我不相信你需要 ; 在 SQL 语句的末尾

于 2013-10-10T19:38:54.533 回答