Closed Thread Icon

Topic awaiting preservation: mysql_fetch_object vs. mysql_fetch_array (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=12542" title="Pages that link to Topic awaiting preservation: mysql_fetch_object vs. mysql_fetch_array (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: mysql_fetch_object vs. mysql_fetch_array <span class="small">(Page 1 of 1)</span>\

 
Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-04-2002 18:18

Why would I want to use mysql_fetch_object when mysql_fetch_array seems to be more versatile? I use mysql_fetch_object in some cases just because the syntax is a bit quicker to type ($object->property instead of $array["key"]), but arrays allow me to do a lot more. Just wondering if there's some special advantage to returning a result row as an object.

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 12-04-2002 18:28

Um not really. It could be usefull if you could extend the class of the object returned but I'm not sure how this would be done for php defined objects.



.:[ Never resist a perfect moment ]:.

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 12-04-2002 18:38

there are some (hypothetical) advantages...

If you were to put all your sql results into an array (bad thing, since it eat's memory, but needs to be done sometimes),
and had objects in there, instead of arrays, your code would be somewhat cleaner
(ie.
myRows[98]->username
instead of
myRows[98]['username']
).

but's that not really a reason...
I personally just think they want us to get confused. Hooray.

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-04-2002 19:11

Well, I'm trying to implement the solution discussed in my previous thread on re-ordering... and I've got some code along the lines of

code:
// $elements is my result set

$final_elements = array();

while ($row = mysql_fetch_array($elements))
{
// if current row has no "previous" listed, then it should be first in the array
if (!$row["previous"])
array_unshift($final_elements, $row);

for ($i = 0; $i < count($final_elements); $i++)
{
if ($final_elements[$i]["id"] == $row["previous"])
array_splice($final_elements, ($i + 1), 0, $row);
else
$final_elements[] = $row;
}
}



I'm still debugging it as we speak, but I think that it'll successfully sort the result set so that each record is placed after the record specified in the "previous" field.

ID 1, previous null
ID 25, previous 1
ID 3, previous 25

and so forth.

I guess the result set could have been read as objects, and then, as you suggested, the main array has objects as its elements.

edit: Wow, that code just crashed Apache. Seems I have some work to do.

edit: Ah, I see... array_splice is not as capable as I hoped it was. I was giving it ambiguous input. Better try something else... hey! Perhaps if I read $row as an object using mysql_fetch_object, I can use array_splice the way I hope to!

edit: Nope, array_splice times out... apparently it's either shockingly slow, or (more likely) I'm simply using it in a way it's not ready for. On to the next thing!

[This message has been edited by Perfect Thunder (edited 12-04-2002).]

Perfect Thunder
Paranoid (IV) Inmate

From: Milwaukee
Insane since: Oct 2001

posted posted 12-04-2002 19:53

Got it! In case anyone cares, the code that worked was

code:
$final_elements = array();

while (count($final_elements) < mysql_num_rows($elements))
{
while ($row = mysql_fetch_object($elements))
{
// see if $row has (previous == null); if so, put it at the beginning of $final_context
if (!$row->previous)
$final_elements[] = $row;

// see if current last element of $final_elements is the "previous" value of $row
// if so, put $row at the end of $final_elements
if ($final_elements[(count($final_elements) - 1)]->id == $row->previous)
$final_elements[] = $row;
}
}

// $final_elements now contains all page elements in order



This is actually much cleaner and faster-running than what I had before, anyway.

« BackwardsOnwards »

Show Forum Drop Down Menu