Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to see if the logged in user is verified. But there is an error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/psmcouk/public_html/colemansystems/verify.php on line 332

Here is the PHP code:

$user1 = $_SESSION['usr'];

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName=$user1");

while($row = mysql_fetch_array($result)) //LINE 332
  {
  $valid = $row['valid'];

  }
  if($valid == "1"){
      echo "$user1, you're account is currently verified.";

  }

I just can not see what is wrong with this code.

Thanks!

share|improve this question
add comment

7 Answers

up vote 2 down vote accepted

All the answers above are lame.

$user1 = mysql_real_escape_string($_SESSION['usr']);
$query = "SELECT valid FROM phpbb_members WHERE memberName='$user1' and valid=1";
$result = mysql_query($query) or trigger_error(mysql_error()." in ".$query);
$valid = mysql_num_rows($result);
if($valid){
  echo "$user1, your account is currently verified.";
}
share|improve this answer
 
Thanks a lot! Worked first time! Much appreciated! –  hart1994 Dec 2 '10 at 11:56
add comment

You probably have an SQL error. Try

if (!$result) {
    echo 'Invalid query: ' . mysql_error() . "\n";
}
share|improve this answer
add comment

I guess $user should be quoted:

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName='$user1'");

You can always see whats wrong my placing echo mysql_error(); after the query

share|improve this answer
add comment

As already posted, you just have to put the user name in single quotations marks:

$query = "SELECT * FROM phpbb_members WHERE memberName = '".$user1."'";

Assuming, that the user name column is varchar. The code you used is only valid if you compare numbers, e.g. integers.

A general remark: Depending on the size of the columns of your database, it might be resonable to select specific rows rather than all using *. For instance:

$query = "SELCT memberName, valid FROM phpbb_members";
share|improve this answer
add comment

Try to use:

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName='$user1'") 
          or die(mysql_error()); // to get if any error exists
share|improve this answer
add comment
$user1 = $_SESSION['usr'];

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName=$user1");

while($row = mysql_fetch_field($result)) //LINE 332
  {
  $valid = $row['valid'];

  }
  if($valid == "1"){
      echo "$user1, you're account is currently verified.";

  }

try this.

share|improve this answer
add comment

You should test the result of mysql_query before using it, if you follow the examples from php.net :

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName=$user1");
if (!$result) {
  die('Request problem : ' . msql_error());
}

while($row = mysql_fetch_array($result)) //LINE 332
...
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.