使用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)
答案 0 :(得分: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);
答案 1 :(得分:1)
您可以使用mysql的count
功能
$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,访问此页面的任何人都可能会从答案中得到它仍然可以接受并且使用安全的印象。
答案 2 :(得分:0)
试试这个:
$sql="SELECT * FROM user WHERE siteAddress='{$siteAddress}'";
花括号允许PHP将$ siteAddress变量的内容嵌入到字符串中。另外,我不相信你需要;在SQL语句的末尾