Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mysql_fetch_array(): supplied argument is not a valid

Status
Not open for further replies.

QuietFire

Programmer
Feb 25, 2003
12
US
Greetings all. I'll admit I'm far more comfortable with asp/sql than php. I know exactly how to acheive the result I'm looking for there.

Anyhow I'm getting a "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /blabla/update_user.php on line 54" error.

I've used a very simular peice of code to loop through results before, but on this one I have to find a particular record based on on field. the main difference is the where statement containing OR.

Here is the code:

$query = "SELECT active,email,username FROM users WHERE (email ='$email' OR username='user_name')";
connectDB();
$r=mysql_query($query);
$num_results=mysql_num_rows($r);
if($num_results > 1){ /*more than one record based on the query, loop for the record with matching username. */
for ($i=0; $i<$num_results;$i++) {
$row = mysql_fetch_array($r);
if($user_name==$row["username"]){
$uname=($row["username"]);
$mail=($row["email"]);
$active=$r=($row["active"]);
}
}
}else{
$uname=$r[username];
$mail=$r;
$active=$r[active];
}
 
The error you got usually indicates that there was an error with the query. I noticed that you are not doing any error checking on your mysql_query() call and just assume it works.

If you change that line to
Code:
$r=mysql_query($query) or die('Error in query: ' . $q . '<br>' . mysql_error());
Is anything printed? If so, it should give you a clue as to the problem.

Ken
 
You need to use this:

$query = "SELECT `active`,email,username FROM users WHERE (email ='$email' OR username='user_name')";

or

$query = "SELECT [active],email,username FROM users WHERE (email ='$email' OR username='user_name')";

Because active is one of the reserved word. I don't know what for, but that make it not working.
 
kenrbnsn,

Good thought for the error reporting, but the query was going through.

Woody,

The error reporting didn't snag the query until I put in th brackets! But I am going to look for a list of resever words to avoid that in the future.

This is going to be a fun one!

Charlie
 
Actually I found the problem:
WHERE (email ='$email' OR username='user_name')";
was missing the $ at ='user_name')
I was getting results somehow anyway!

Not only that I found some flaws in my logic. If the e-mail already exists, it doesn't matter if the username was taken by someone else, the person with the e-mail already has a record!

Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top