FuegoFingers
FuegoFingers

Reputation: 2199

mysql_fetch_array() works but mysql_fetch_object() doesn't

I have the following code which works:

$date1 = mysql_query("SELECT date1 FROM Users WHERE username='".$_SESSION['username']."'") 
    or die(mysql_error());
while($row = mysql_fetch_array($date1)){
    $lastViewedDate = $row[0];
}

But when I try to use mysql_fetch_object I get an Internal Server Error 500.

$date1 = mysql_query("SELECT date1 FROM Users WHERE username='".$_SESSION['username']."'") 
    or die(mysql_error());
while($row = mysql_fetch_object($date1)){
    $lastViewedDate = $row["date1"];
}

I would prefer to use the objects in the future but I don't understand why they aren't working. Any help would be appreciated.

Upvotes: 0

Views: 242

Answers (3)

Evert
Evert

Reputation: 99599

With fetch_object the syntax is :

$row->date1 

with fetch_assoc the syntax is:

$row["date1"]

Also please.. for everything that is holy.. Turn E_NOTICE error messaging and display_errors on

Upvotes: 4

andrewsi
andrewsi

Reputation: 10732

$lastViewedDate = $row["date1"];

That's not how you access an object's variables; try this:

$lastViewedDate = $row->date1;

Upvotes: 1

Mike Brant
Mike Brant

Reputation: 71384

You are not accessing your object correctly it would be like this:

$date1 = mysql_query("SELECT date1 FROM Users WHERE username='".$_SESSION['username']."'") 
    or die(mysql_error());
while($row = mysql_fetch_object($date1)){
    $lastViewedDate = $row->date1;
}

Note the use of the object property accessor ->

Upvotes: 1

Related Questions