drifter Posted December 23, 2006 Share Posted December 23, 2006 I was just doing some more mysql tweaking, and I was wondering about the options to return a row as a object or an assoc array. What are the differences? I use assoc 95% of the time. Is it just a matter or preference, or is there really a difference.Thanks Link to comment https://forums.phpfreaks.com/topic/31656-assoc-array-or-objects/ Share on other sites More sharing options...
trq Posted December 23, 2006 Share Posted December 23, 2006 I think its mostly personal preference. Though I know you can name the returned object so I would assume you may also be able to extend it, never played with that though. Could be interesting, but then again, Ide imagine it would make your code pretty hard to read. Link to comment https://forums.phpfreaks.com/topic/31656-assoc-array-or-objects/#findComment-146725 Share on other sites More sharing options...
trq Posted December 23, 2006 Share Posted December 23, 2006 Ooh... Very interesting, check this example out.[code]#!/usr/bin/php<?php class user { function test() { echo "this is a test"; } } mysql_connect('localhost','thorpe','****'); mysql_select_db('foo'); $sql = "SELECT id, val FROM bar WHERE val = 'thorpe'"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result) > 0) { $obj = mysql_fetch_object($result,'user'); echo $obj->val; // produces 'thorpe' } } else { echo mysql_error(); } $obj->test(); // produces 'this is a test'?>[/code]This basically meens that you can create a class, with methods already in place to manipulate its properties. You can then run a query and use mysql_fetch_object to populate this objects properties. This could come in REAL handy.Thanks. Ide never even thought about it untill you asked the question. Link to comment https://forums.phpfreaks.com/topic/31656-assoc-array-or-objects/#findComment-146730 Share on other sites More sharing options...
drifter Posted December 23, 2006 Author Share Posted December 23, 2006 that sounds like it may be very useful in the right spots. Link to comment https://forums.phpfreaks.com/topic/31656-assoc-array-or-objects/#findComment-146732 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.