0

Hi
when I run my code the loop doesn't stop! which force the browser to freez.I don't know what is wrong with the loop
I have around 4 users only !
and my query select only one of them

here is my code :

    $id    = $_GET['id'];
    $sql   = "SELECT * FROM users WHERE id='".$id."'";
    $query = mysql_query($sql) or die(mysql_error());
    $check = mysql_fetch_array($query);
   if($check[0]>0) {

    while($row = $check ) {
        $username = $row['username'];
        echo $username;

}
3
Contributors
2
Replies
30
Views
3 Years
Discussion Span
Last Post by cereal
0

Since you are going to fetch only one row from DB table regarding to requested id by method get, it would be more convenient to use mysql_fetch_row() function.

Also make check for your input, something like:

$id = isset($_GET['id'] && (int)($_GET['id']) !== FALSE) ? $_GET['id'] : 'exit';

if ($id === 'exit')
{
    header('Location: http://www.example.com/'); // or some predefined page in this case
    exit();
}
//rest of your code here
1

A small correction: you cannot compare booleans with integers, otherwise you get unexpected results. An example:

$a['foo'] = FALSE;
echo (int)$a['foo'] !== FALSE ? 'true':'false';

The problem here is given by (int). The comparison will translate to 0 !== FALSE which returns true instead of false, the same happens if the value is NULL.

Votes + Comments
Thanks for clarification.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.