Jump to content

Archived

This topic is now archived and is closed to further replies.

merylvingien

mysql_num_rows error?

Recommended Posts

Hi guys, one of the last questions from me for a while i hope lol

 

i am trying to check if a phone number allready exists in a database using mysql_num_rows

 

 

The code is this:

$SQL = "SELECT * FROM postcode WHERE phone = $phone";
	$result = mysql_query($SQL);
	$num_rows = mysql_num_rows($result);

	if ($num_rows > 0) {
		$errorMessage = "It seems that this phone number is already been entered into our database";
	}

	else {bla bla bla

 

But i keep getting a warning message: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in

 

I am at my wits end with this and done a lot of searching to try and find out why this is, but obviously i am dim and need it to be explained in english.

Cheers!

Share this post


Link to post
Share on other sites

You're missing your connection resource from your mysql_query. mysql_query should be done like this:

 

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);

 

notice the link after the query. without that $result will not be a valid result resource and therefore mysql_num_rows will fail too.

Share this post


Link to post
Share on other sites

You're missing your connection resource from your mysql_query. mysql_query should be done like this:

 

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);

 

notice the link after the query. without that $result will not be a valid result resource and therefore mysql_num_rows will fail too.

 

Thats not true, it may be best practice to use the connection resource parameter but it is not required. The error is being caused because mysql_query is returning false meaning the query is failing. I would guess that $phone is of type varchar, which would cause a syntax error as it is not enclosed in single quotes.

Share this post


Link to post
Share on other sites

Thanks for the replies fellas, i enclosed the $phone in single quotes and that has made the error go away, BUT when i do a trial run and add a phone number that same as one that is already in the test database, it just seems to ignore the fact that the phone number exists at all.  :wtf:

 

Here is the full code:

<?php
$fname= $_POST['fname'];
$fname= mysql_real_escape_string($fname, $con);
$phone= $_POST['phone'];
$phone= mysql_real_escape_string($phone, $con);
$mobile= $_POST['mobile'];
$mobile= mysql_real_escape_string($mobile, $con);
$email= $_POST['email'];
$email= mysql_real_escape_string($email, $con);
$weblink= $_POST['weblink'];
$weblink= mysql_real_escape_string($weblink, $con);
$linktitle= $_POST['linktitle'];
$linktitle= mysql_real_escape_string($linktitle, $con);
$advert= $_POST['advert'];
$advert= mysql_real_escape_string($advert, $con);
$pagestate="Taken!";




	$SQL = "SELECT * FROM postcode WHERE phone = '$phone'";
	$result = mysql_query($SQL);
	$num_rows = mysql_num_rows($result);

	if ($num_rows > 0) {
		$errorMessage = "It seems that this phone number is already been entered into our database";
	}

	else {
     
if(isset($_POST['signupID'])) {
foreach($_POST['signupID'] as $item) {
$sql = "UPDATE postcode SET pagestate='$pagestate', fname='$fname', phone='$phone', mobile='$mobile', email='$email', weblink='$weblink', linktitle='$linktitle', advert='$advert'  WHERE postcodeID=$item";
      mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);
      $result = mysql_query($sql);
}	  
}
}

echo "Thank you for signing up!<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />";


?>

 

edit: GKWelding, i tried your suggestion and this threw 2 errors up: Warning: mysql_query() expects parameter 2 to be resource, boolean

and :Warning: mysql_num_rows() expects parameter 1 to be resource, null given

Share this post


Link to post
Share on other sites

Maybe you should filter the result for the phone that comes from post, to use a regex to clear the wrong chars from it like space, quotes, etc, to make sure to the comparison it goes clear.

Share this post


Link to post
Share on other sites

Hi dymon, thats the next step, but as i am learning i am taking it once pace at a time. Its all starting to get a little complex as it is LOL

 

I was thinking it would probably be a better thing to check ip address rather than phone number anyway, but i would like to get this sorted out as to why it doesnt work. I am entering the number personaly so i know it is identical to the one in the database, but it still goes ahead and enters the data, even though the phone number already exists!

 

Basicly this is stop a user entering data twice, as there is a limit on how many spaces they can take.

Share this post


Link to post
Share on other sites

Hi

 

Echo out $SQL and try it directly in phpmyadmin or equivalent.

 

All the best

 

Keith

Share this post


Link to post
Share on other sites

The two values obviously aren't equal otherwise the code you have should work. Does your postcode table have a unique id column? If so pick one at random and select it using mysql.

 

$result = mysql_query("SELECT phone FROM postcode WHERE id=10");
$row = mysql_fetch_assoc($result);

echo urlencode($row['phone']); // note I just call the urlencode so you can see anywhitespace chars
echo urlencode($_POST['phone']);
// or even better

if($row['phone'] === $_POST['phone']) {
   echo "match";
}

Chances are theres an odd character there somewhere.

Share this post


Link to post
Share on other sites

You would better print both values from the database and from _POST, so you will be able to make a visual comparison, and of course you can place them here for us to see too.

Share this post


Link to post
Share on other sites

The two values obviously aren't equal otherwise the code you have should work. Does your postcode table have a unique id column? If so pick one at random and select it using mysql.

 

$result = mysql_query("SELECT phone FROM postcode WHERE id=10");
$row = mysql_fetch_assoc($result);

echo urlencode($row['phone']); // note I just call the urlencode so you can see anywhitespace chars
echo urlencode($_POST['phone']);
// or even better

if($row['phone'] === $_POST['phone']) {
   echo "match";
}

Chances are theres an odd character there somewhere.

 

Thats done the trick, i thank you oh wise one!!!

 

Still cannot understand why the mysql_mun_rows didnt work though!

 

I think i am almost to the end of this daunting project regards to coding and i would like to thank all of those who have helped me out along the way! Its most appreciated.

Hopefully i will be able to contribute something back whence i have actually learned somthing LOL

Share this post


Link to post
Share on other sites

Dear Fellows...

 

I got the Same Error "mysql_num_rows() expects parameter 1 to be resource, null given"

 

The tips given Here was Useful, very much... I think the database record/s we are accessing has an white space at the beginning, then the query does not match them....

 

Example:

"SELECT * FROM `data` WHERE `id` LIKE '888%' "

then this will give the error... but when we edit this as, with whitespace before 888

"SELECT * FROM `data` WHERE `id` LIKE ' 888%' "

 

It Worked!

Check this out.. It may be a solution....

Share this post


Link to post
Share on other sites

pls help me..

i cant fix the problem

Could not successfully run query () from database

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in on line 78

17293_.php

17294_.php

Share this post


Link to post
Share on other sites

wizzle, I commend you for searching the forum to find your error message in an attempt to solve your problem. But, because your code is unique to what you are doing, the cause of your error is not necessarily the same as the last post in this thread, which itself is an add-on post to this thread with a different error and a different cause from the original post that started this thread. You always need to start your own thread for your own problems. If you want to refer to a similar thread, just post a link to it in your own thread.

 

The original error message for the OP that started this thread is different from your error message. The error message for ADynaMic's post does match your error message, but his actual code, which he didn't post, is producing that error for a different reason than your code. I can guarantee this based on his analysis of the change he made to the query as being the cause of the problem. His error is not directly related to the change in the query that he posted. It is some kind of follow-on error later in his code.

 

----------------------------

 

Now to your actual problem.

 

Your code is supplying a null to the mysql_num_rows() statement because the $result variable doesn't exist. The reason it doesn't exist is because your switch/case statement is not matching any of the three choices and $result has not been created at all.

 

That would indicate that your $select variable does not contain one of the expected values. You need to troubleshoot your code to find out what is in $select and then find out why it doesn't have an expected value in it or it doesn't have any value in it (i.e. empty.) You also need to add logic in your code so that you don't attempt to use the $result variable if it doesn't contain a valid result resource as the result of executing one of your mysql_query() statements.

 

To see what the $select variable contains, use - var_dump($select); right before the start of your switch/case statement. If it contains a value, but it is not one of the three choices, you would need to troubleshoot why your form is not supplying a valid number. If it is empty, you need to determine why $_POST["search_type"] either doesn't exist or doesn't contain anything.

 

Since the code you posted is supposed to be processing form data, you also need to add logic to your code to test that a form was submitted at all so that your form processing code only executes when there is $_POST data from the form.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.