简体   繁体   中英

PHP - Mysql query problem

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!

You probably have an SQL error. Try

if (!$result) {
    echo 'Invalid query: ' . mysql_error() . "\n";
}

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.";
}

Try to use:

$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName='$user1'") 
          or die(mysql_error()); // to get if any error exists

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

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, eg 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";
$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.

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
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM