简体   繁体   中英

General debugging

I have code that echoes out someone's first name and it works with some id's but not with others. Here is the code:

$sid = $_SESSION['id'];
$sql = mysql_query("SELECT * FROM users WHERE id='$sid'");
$row = mysql_fetch_array($sql);
$fname = $row['fname'];
.
.
.
<?php echo $fname; ?>'s Profile

Any idea how this can work only some of the time? I am using PHP with MySQL. I echoed out $sid and it is giving the right id.

You are not checking if the query is returning any result.

Do a check first:

if ($sql) { // the query worked,
    while($row = mysql_fetch_array($sql)) {
        $fname = $row['fname'];
        ....
    }
}else {
    .. there was an error ...
    echo "Error: ".mysql_error();
}

You can do a var_dump($row) to see the content.

It is possible that the value IS coming from the database column and contains an empty string or is NULL. You should examine the database directly with a specific id to determine what to expect SPECIFICALLY.

That would explain your results.

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