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!
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.";
}
You probably have an SQL error. Try
if (!$result) {
echo 'Invalid query: ' . mysql_error() . "\n";
}
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, 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";
Try to use:
$result = mysql_query("SELECT * FROM phpbb_members WHERE memberName='$user1'")
or die(mysql_error()); // to get if any error exists
$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
...
精彩评论