简体   繁体   中英

Php Select statement issue with mysql

Php Select statement issue with mysql

I get this error ..

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

My Code is

$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";
}

I try echo $sql and I get this

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';

If i input the sql at phpmyadmin it return something..

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

you have two semi-colons there

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

it should be:

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

you can do also:

$sql= mysql_query("SELECT * FROM user WHERE siteAddress='" . $siteAddress ."'");
$count=mysql_num_rows($sql);

You could use the count feature of mysql

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

or broken down

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

I've used mysqli in the example as mysql is deprecated and anyone visiting this page may get the impression from the answers that it is still acceptable and safe to use.

try this:

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

The curly braces allow PHP to imbed the content of the $siteAddress variable into the string. Also, I don't believe you need the ; at the end of the SQL statement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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