FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Mysql error: Warning: mysql_num_rows():





DanielXP
I keep getting this error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/danielxp/public_html/Plus News 2/display.php on line 209

with this code
Code:
case "addcomment":
$id = (int)$_GET['id'];
$name = htmlspecialchars($_POST[name]);
$comment = htmlspecialchars($_POST[comment]);
$postdate = date("j F Y");
$ip = $_SERVER['REMOTE_ADDR'];
if($name==NULL|$comment==NULL) {
echo "A field was left blank.";
}
else
$checkname = mysql_query("SELECT username FROM users WHERE username='$name'");

$checkname= mysql_num_rows($checkname); <-- This line

if ($checkname>0) {
$name = $_POST[name];
$comment = $_POST[comment];
echo "<input name='name' type='hidden' value='$name'>
<input name='content' type='hidden' value='$content'>";
echo "This name is password protected, Please enter you password below";
}
break;
Traveller
You have duplicated the variable $checkname. The first time, you DID use it as a result resource when you ran the query. The second time (the line with the error message), you used it as a number to receive the number of rows. Change it to something like $namecount (and change the subsequent references), and it should work.
DanielXP
$checkname = mysql_query("SELECT username FROM users WHERE username='$name'");
$namecount = mysql_num_rows($checkname);
if ($namecount>0) {

Still same error Sad
Traveller
It could also be that the query failed for some reason. Try adding a "die" statement after the query. See the various, online tutorials, such as http://www.w3schools.com, if you need further information about "die".

In addition, I would suggest constructing the query ahead of time so you can print it out to see what is REALLY trying to be done:
Code:
$namequery= "SELECT username FROM users WHERE username='$name'";
echo "<br>".$namequery."<br>";
$checkname = mysql_query($namequery) or die("Error encountered during query");
SlowWalkere
Does the script think that you left one of the fields blank?

When you check if either the name or comment field is blank, you have the query, and only the query, execute in the "else" portion. So, if the script enters the if statement, evaluates true, and echo's "A field was left blank," the query would never execute. In that case, you would call mysql_num_rows and $checkname would not be a resource - hence the error. But that's just a stab in the dark.

That could be caused because you're using a bitwise or (|) instead of a regular or (||). I don't know why it would do that, but bitwise operators do wierd things sometimes.

I'm also guessing that the end of the script is only supposed to execute if both fields were filled, so you'll need to add brackets for the "else".

- Walkere
avk
Hey! where is your connection to the database ...

I think that it had caused the problem for the mysql_num_rows.

Or there is some problem with your query.

Just try to check your query and then reply.
yjwong
There is a high chance that the query has a problem. Please check your query and your database structure. To check what error has been encountered, use mysql_error(resource link_identifier).
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.