Fasand Fasand - 4 years ago 89
SQL Question

Retrieving information from database (1 field/object; no arrays)

I'm doing a sort of login page for my website which I'm just testing right now, and this code is after the login. The thing is, I want to retrieve some information from my database, but the code doesn't work (it doesn't echo anything). I checked that the MySQL query is fine and it works, but there is no result in the PHP.

Please I would be happy for any help and answers,

//---MySQL connection---//
$server = "localhost";
$user = "root";
$pass = "password";
$db = "users";
$table = "users";
mysql_connect($server,$user,$pass) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
//----------------------//

//---MySQL query, data selection---//
$sesuser = $_SESSION['username'];
$sql = "SELECT data FROM $table WHERE username='$sesuser'";
$predata = mysql_query($sql);
$data = mysql_fetch_field($predata);
//---------------------------------//

//---Check if session is registered---//
session_start();
if(session_is_registered("username")){
echo "\n"."Hello ".$_SESSION["username"]."<br />";
echo $data; //!!this line doesn't work
}
else{
echo "<script>window.location=/login/</script>";
}
//------------------------------------//
?>

Answer Source

var_dump($data); - What is says?

And YES, but session_start at begining of file;

And try(via php):

$i = 0;
while ($i < mysql_num_fields($result)) {
    echo "Information for column $i:<br />\n";
    $meta = mysql_fetch_field($result, $i);
    if (!$meta) {
        echo "No information available<br />\n";
    }
    echo "<pre>
blob:         $meta->blob
max_length:   $meta->max_length
multiple_key: $meta->multiple_key
name:         $meta->name
not_null:     $meta->not_null
numeric:      $meta->numeric
primary_key:  $meta->primary_key
table:        $meta->table
type:         $meta->type
unique_key:   $meta->unique_key
unsigned:     $meta->unsigned
zerofill:     $meta->zerofill
</pre>";
    $i++;
}

And if you change mysql_fetch_field to mysql_fetch_row you would be able to reach your data over: $data[0];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download