|
Posted by Jerry Stuckle on 04/02/07 14:29
TKirahvi wrote:
>> What values does $db->f('id') return? Are there really two records? Is
>> $db's class doing what it's supposed to?
>>
>> The problem does not lie in the assignment to $galleries[], that's for
>> sure. So the problem is elsewhere in your code, or the concept behind
>> it. Post more details, and test your snippet by echoing intermediate
>> results to screen. Perhaps there's a good clue.
>>
>
> If I echo($db-f('id')); within while loop, it echoes correct id
> numbers (in this case 1 and 2), as so:
>
> while( $db->next_record() )
> {
> $foo = $db->f('id');
> echo($foo);
> }
>
> output: 1 2
>
> But if I create Gallery Objects within the loop, I guess only first
> one is created properly:
>
> while( $db->next_record() )
> {
> $foo = $db->f('id');
> gallery = new Gallery($foo);
> echo($gallery->id);
> }
>
> output: 1
>
> So it seems Arrays has nothing to do with this. I wonder if Gallery
> Object is missing something crucial, I'm not that familiar with PHP's
> Objects. My Gallery Object is:
>
> class Gallery
> {
> var $id;
> var $name;
> var $description;
> var $owner;
> var $authority;
> var $date;
>
> function __construct($id)
> {
> global $db;
>
> $select = 'SELECT * FROM gallery WHERE id = '.$id;
> $result = $db->query( $select );
>
> if ( !$result) {
> echo( 'Query failed');
> }
> else
> {
> $db->next_record();
> $this->id = $db->f("id");
> $this->name = $db->f("name");
> $this->description = $db->f("description");
> $this->owner = $db->f("owner");
> $this->authority = $db->f("authority");
> $this->date = $db->f("date");
> }
> }
>
> function __get($v)
> {
> return $this->$v;
> }
> }
>
Your problem is you're reusing $db in your class. When you issue:
$result = $db->query( $select );
You have wiped out the recordset you're using in:
while( $db->next_record() )
If you're going to make another request, you need to use another object.
A real good reason why you shouldn't use globals.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
[Back to original message]
|