0

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)
6
  • No, it doesn't return anything . Commented Oct 10, 2013 at 19:35
  • echo mysql_error() to see the error message. Commented Oct 10, 2013 at 19:36
  • It show my 1 record below.. haha :) got record but just at php i dont know why got that error, I just wanna check if no record return, then show error message. is there something wrong with mysql_num_rows? Commented Oct 10, 2013 at 19:36
  • 1
    There's nothing wrong with mysql_num_rows, the problem is that mysql_query is getting an error. You need to look at its error message. Commented Oct 10, 2013 at 19:37
  • Where are you connecting to the database? You should have conditionals all throughout your code (when you are connecting to database, when you are querying, etc.) that look for unexpected results and can then log the error when you are not getting what you expect. Commented Oct 10, 2013 at 20:05

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

1 Comment

curly braces are not going to make any difference here (other than maybe help readability).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.