Speciality in mysql_fetch_array
Speciality in mysql_fetch_array
am 26.11.2007 10:29:13 von pradeep
Dear all,
I want to know the difference between mysql_fetch_array() and
mysql_fetch_row().
I know that mysql_fetch_row() return data in numeric array while
mysql_fetch_array() return data in numeric as well as in associative
array.
But my question is, why it returns in both format. Is it wastage of
memory ?
Why this wastage is necesarry ?
Thanks in advance.
BYe.
Re: Speciality in mysql_fetch_array
am 26.11.2007 11:22:03 von Erwin Moller
pradeep wrote:
> Dear all,
>
> I want to know the difference between mysql_fetch_array() and
> mysql_fetch_row().
>
> I know that mysql_fetch_row() return data in numeric array while
> mysql_fetch_array() return data in numeric as well as in associative
> array.
>
> But my question is, why it returns in both format. Is it wastage of
> memory ?
> Why this wastage is necesarry ?
Hi,
The answer is simple, if you want an assoc array, you can use
mysql_fetch_array().
I always prefer constructs like this in my code:
echo $row["firstname"];
above
echo $row[3];
Since the former leaves you without doubt which column you are using.
And also: If you expand your query to get more columns, an index of an
array might easily get you confused.
But if you like the array-indexes more, just use them.
I expect there must be less overhead involved when no assoc arrays are
created and returned.
So as far as I can tell: it is not a waste of memory, but it offers you
the choice what you want:
1) faster code/less IO (use only indexes)
2) better readable/more robust code. (use assoc arrays)
just my 2 cent.
Regards,
Erwin Moller
>
> Thanks in advance.
>
> BYe.
Re: Speciality in mysql_fetch_array
am 26.11.2007 12:22:26 von Captain Paralytic
On 26 Nov, 09:29, pradeep wrote:
> Dear all,
>
> I want to know the difference between mysql_fetch_array() and
> mysql_fetch_row().
>
> I know that mysql_fetch_row() return data in numeric array while
> mysql_fetch_array() return data in numeric as well as in associative
> array.
So you know the difference then
>
> But my question is, why it returns in both format. Is it wastage of
> memory ?
It returns in both formats because that is what it was built to do.
>Is it wastage of memory ?
It is only a waste if you don't need both array types. If you only
want numeric use mysql_fetch_row(), or you can use msql_fetch_array()
with the MSQL_NUM constant. If you only want associative then use
MSQL_ASSOC.
> Why this wastage is necesarry ?
>
> Thanks in advance.
>
> BYe.
Re: Speciality in mysql_fetch_array
am 26.11.2007 13:55:22 von michael.martinek
> So as far as I can tell: it is not a waste of memory, but it offers you
> the choice what you want:
> 1) faster code/less IO (use only indexes)
> 2) better readable/more robust code. (use assoc arrays)
>
> just my 2 cent.
>
> Regards,
> Erwin Moller
And I fully agree with you, Erwin.
As for the general topic, if you want to be able to expand on your
code later, without having to worry about reviewing every SQL query..
use associative arrays. For example, if you have a SQL query 'SELECT *
FROM table WHERE something'.. then your numerical indexed array is
going to be specific. But what happens if a DB admin inserts a new
column somewhere, or drops one? Now you'll need to review all the
code.. and good luck with that, because $row[3] is harder to find
meaning in than $row['data_crc']. And seriously, we're in a day and
age where using a little extra memory here or there isn't going to do
any harm. In a business scenario, it'll likely cost you more to be as
system efficient as you possibly can.
I always use mysql_fetch_assoc, unless I'm doing a "SELECT
COUNT(*) ..."
If it's a script you know is going be extremely intensive to run, and
using some extra storage here and there is going to bog down resources
then use fetch_array.. but if that's the case, you might want to
upgrade. ;) Also keep in mind, signed integers aren't free.. and if
you're really worried about resources, do an unset() on variables when
they are large arrays and contain many entries. When passing arrays to
a function, pass them by reference so a new copy isn't made.
My 1/50th of a dollar in summation:
If you're writing code for a business, contract work, or something
that will need to be maintained then use associative arrays. Otherwise
if you're writing a quick one-time usage script that probably won't
need to be maintained, expanded on, debugged.. then go ahead and used
numerically indexed arrays. However, the speed increase over
associative arrays in normal circumstances is not worth using as a
reason to do so.
Re: Speciality in mysql_fetch_array
am 26.11.2007 14:15:50 von Jerry Stuckle
Erwin Moller wrote:
> pradeep wrote:
>> Dear all,
>>
>> I want to know the difference between mysql_fetch_array() and
>> mysql_fetch_row().
>>
>> I know that mysql_fetch_row() return data in numeric array while
>> mysql_fetch_array() return data in numeric as well as in associative
>> array.
>>
>> But my question is, why it returns in both format. Is it wastage of
>> memory ?
>> Why this wastage is necesarry ?
>
> Hi,
>
> The answer is simple, if you want an assoc array, you can use
> mysql_fetch_array().
>
> I always prefer constructs like this in my code:
> echo $row["firstname"];
>
> above
>
> echo $row[3];
>
> Since the former leaves you without doubt which column you are using.
> And also: If you expand your query to get more columns, an index of an
> array might easily get you confused.
>
> But if you like the array-indexes more, just use them.
> I expect there must be less overhead involved when no assoc arrays are
> created and returned.
>
> So as far as I can tell: it is not a waste of memory, but it offers you
> the choice what you want:
> 1) faster code/less IO (use only indexes)
> 2) better readable/more robust code. (use assoc arrays)
>
> just my 2 cent.
>
> Regards,
> Erwin Moller
>
>>
>> Thanks in advance.
>>
>> BYe.
>
Erwin,
The overhead in creating an associative array is negligible.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Speciality in mysql_fetch_array
am 26.11.2007 14:19:49 von Jerry Stuckle
Michael Martinek wrote:
>> So as far as I can tell: it is not a waste of memory, but it offers you
>> the choice what you want:
>> 1) faster code/less IO (use only indexes)
>> 2) better readable/more robust code. (use assoc arrays)
>>
>> just my 2 cent.
>>
>> Regards,
>> Erwin Moller
>
> And I fully agree with you, Erwin.
>
>
> As for the general topic, if you want to be able to expand on your
> code later, without having to worry about reviewing every SQL query..
> use associative arrays. For example, if you have a SQL query 'SELECT *
> FROM table WHERE something'.. then your numerical indexed array is
> going to be specific. But what happens if a DB admin inserts a new
> column somewhere, or drops one? Now you'll need to review all the
> code.. and good luck with that, because $row[3] is harder to find
> meaning in than $row['data_crc']. And seriously, we're in a day and
> age where using a little extra memory here or there isn't going to do
> any harm. In a business scenario, it'll likely cost you more to be as
> system efficient as you possibly can.
>
SELECT * is not a good idea. You should always specify the columns you
want, even if it's all current columns in the table. What happens if
someone adds a column with 2MB BLOBs, for instance? Or deletes a
column? SELECT * will take a lot of memory in the first case and NOT
get an error in the second - creating a possible difficult problem to find.
> I always use mysql_fetch_assoc, unless I'm doing a "SELECT
> COUNT(*) ..."
>
> If it's a script you know is going be extremely intensive to run, and
> using some extra storage here and there is going to bog down resources
> then use fetch_array.. but if that's the case, you might want to
> upgrade. ;) Also keep in mind, signed integers aren't free.. and if
> you're really worried about resources, do an unset() on variables when
> they are large arrays and contain many entries. When passing arrays to
> a function, pass them by reference so a new copy isn't made.
>
> My 1/50th of a dollar in summation:
> If you're writing code for a business, contract work, or something
> that will need to be maintained then use associative arrays. Otherwise
> if you're writing a quick one-time usage script that probably won't
> need to be maintained, expanded on, debugged.. then go ahead and used
> numerically indexed arrays. However, the speed increase over
> associative arrays in normal circumstances is not worth using as a
> reason to do so.
>
Good advice.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: Speciality in mysql_fetch_array
am 26.11.2007 15:58:02 von Erwin Moller
Jerry Stuckle wrote:
> Erwin Moller wrote:
>> pradeep wrote:
>>> Dear all,
>>>
>>> I want to know the difference between mysql_fetch_array() and
>>> mysql_fetch_row().
>>>
>>> I know that mysql_fetch_row() return data in numeric array while
>>> mysql_fetch_array() return data in numeric as well as in associative
>>> array.
>>>
>>> But my question is, why it returns in both format. Is it wastage of
>>> memory ?
>>> Why this wastage is necesarry ?
>>
>> Hi,
>>
>> The answer is simple, if you want an assoc array, you can use
>> mysql_fetch_array().
>>
>> I always prefer constructs like this in my code:
>> echo $row["firstname"];
>>
>> above
>>
>> echo $row[3];
>>
>> Since the former leaves you without doubt which column you are using.
>> And also: If you expand your query to get more columns, an index of an
>> array might easily get you confused.
>>
>> But if you like the array-indexes more, just use them.
>> I expect there must be less overhead involved when no assoc arrays are
>> created and returned.
>>
>> So as far as I can tell: it is not a waste of memory, but it offers
>> you the choice what you want:
>> 1) faster code/less IO (use only indexes)
>> 2) better readable/more robust code. (use assoc arrays)
>>
>> just my 2 cent.
>>
>> Regards,
>> Erwin Moller
>>
>>>
>>> Thanks in advance.
>>>
>>> BYe.
>>
>
> Erwin,
>
> The overhead in creating an associative array is negligible.
>
Hi Jerry,
I expected so much, but never actually measured it, since I always use
assoc retrieval of columns for clearity of code.
A task must be very daunting to optimize in this kind of way.
But thanks for the info. Now I know I'll never use indexed row
retrieval. ;-)
Regards,
Erwin