user1777711
user1777711

Reputation: 1744

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)

Upvotes: 0

Views: 335

Answers (3)

anbow
anbow

Reputation: 11

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.

Upvotes: 1

Al3x_F4nt0m
Al3x_F4nt0m

Reputation: 46

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

Upvotes: 1

Lionel Morrison
Lionel Morrison

Reputation: 596

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

Upvotes: 0

Related Questions