FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » mysql_fetch_array
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
mysql_fetch_array [message #169768] Sat, 25 September 2010 20:28 Go to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
I have the following code:

$query = "SELECT * FROM classics";
$result = mysql_query($query);

if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);

for ($j = 0 ; $j < $rows ; ++$j)
{
$results[] = mysql_fetch_array($result);
}

mysql_close($db_server);

My question, in the loop, why does tha author use:

$results[] = mysql_fetch_array($result);

instead of (as I would expect):

$results[$j] = mysql_fetch_array($result);?

What PHP magic is at work here?

Thanks.
Re: mysql_fetch_array [message #169770 is a reply to message #169768] Sat, 25 September 2010 21:08 Go to previous messageGo to next message
Marious Barrier is currently offline  Marious Barrier
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
On 09/25/2010 04:28 PM, MikeB wrote:
> My question, in the loop, why does tha author use:
>
> $results[] = mysql_fetch_array($result);
>
> instead of (as I would expect):
>
> $results[$j] = mysql_fetch_array($result);?
>
> What PHP magic is at work here?

In PHP, if you don’t specify a key when adding values to an array, it
automatically appends a new key.

$arr = array('a', 'b', 'c');

$arr[] = 'd';

print_r($arr);



array(4)
{
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'd'
}
Re: mysql_fetch_array [message #169771 is a reply to message #169768] Sat, 25 September 2010 21:50 Go to previous messageGo to next message
TechieOldFox is currently offline  TechieOldFox
Messages: 1
Registered: September 2010
Karma: 0
Junior Member
On Sep 26, 4:28 am, MikeB <mpbr...@gmail.com> wrote:
> I have the following code:
>
> $query = "SELECT * FROM classics";
> $result = mysql_query($query);
>
> if (!$result) die ("Database access failed: " . mysql_error());
> $rows = mysql_num_rows($result);
>
> for ($j = 0 ; $j < $rows ; ++$j)
> {
>         $results[] = mysql_fetch_array($result);
>
> }
>
> mysql_close($db_server);
>
> My question, in the loop, why does tha author use:
>
> $results[] = mysql_fetch_array($result);
>
> instead of (as I would expect):
>
> $results[$j] = mysql_fetch_array($result);?
>
> What PHP magic is at work here?
>
> Thanks.

$results[] is equivalent to "add record". SO if you assign a value to
it, it will add a new record in $results for you.
Re: mysql_fetch_array [message #169772 is a reply to message #169768] Sat, 25 September 2010 22:50 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Sep 25, 9:28 pm, MikeB <mpbr...@gmail.com> wrote:
> I have the following code:
>
> $query = "SELECT * FROM classics";
> $result = mysql_query($query);
>
> if (!$result) die ("Database access failed: " . mysql_error());
> $rows = mysql_num_rows($result);
>
> for ($j = 0 ; $j < $rows ; ++$j)
> {
>         $results[] = mysql_fetch_array($result);
>
> }
>
> mysql_close($db_server);
>
> My question, in the loop, why does tha author use:
>
> $results[] = mysql_fetch_array($result);
>
> instead of (as I would expect):
>
> $results[$j] = mysql_fetch_array($result);?
>
> What PHP magic is at work here?
>
> Thanks.

I can recommend reading:
http://php.net/manual/en/language.types.array.php
Re: mysql_fetch_array [message #169773 is a reply to message #169772] Sat, 25 September 2010 23:36 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Captain Paralytic wrote:
> On Sep 25, 9:28 pm, MikeB<mpbr...@gmail.com> wrote:
>> I have the following code:
<snip>
>>
>> What PHP magic is at work here?
>>
>> Thanks.
>
> I can recommend reading:
> http://php.net/manual/en/language.types.array.php


I've read that page before, but only now the following sentence makes
sense:

"If a key is not specified for a value, the maximum of the integer
indices is taken and the new key will be that value plus 1. If a key
that already has an assigned value is specified, that value will be
overwritten. "
Re: mysql_fetch_array [message #169774 is a reply to message #169773] Sun, 26 September 2010 11:44 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Sep 26, 12:36 am, MikeB <mpbr...@gmail.com> wrote:
> Captain Paralytic wrote:
>> On Sep 25, 9:28 pm, MikeB<mpbr...@gmail.com>  wrote:
>>> I have the following code:
>   <snip>
>
>>> What PHP magic is at work here?
>
>>> Thanks.
>
>> I can recommend reading:
>> http://php.net/manual/en/language.types.array.php
>
> I've read that page before, but only now the following sentence makes
> sense:
>
> "If a key is not specified for a value, the maximum of the integer
> indices is taken and the new key will be that value plus 1. If a key
> that already has an assigned value is specified, that value will be
> overwritten. "

I was referring to the section just below headed:
"Creating/modifying with square bracket syntax"
but the paragraph that you mentioned certainly explains more of the
actual details.
Re: mysql_fetch_array [message #169790 is a reply to message #169774] Sun, 26 September 2010 23:24 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 9/26/2010 7:44 AM, Captain Paralytic wrote:
> On Sep 26, 12:36 am, MikeB<mpbr...@gmail.com> wrote:
>> Captain Paralytic wrote:
>>> On Sep 25, 9:28 pm, MikeB<mpbr...@gmail.com> wrote:
>>>> I have the following code:
>> <snip>
>>
>>>> What PHP magic is at work here?
>>
>>>> Thanks.
>>
>>> I can recommend reading:
>>> http://php.net/manual/en/language.types.array.php
>>
>> I've read that page before, but only now the following sentence makes
>> sense:
>>
>> "If a key is not specified for a value, the maximum of the integer
>> indices is taken and the new key will be that value plus 1. If a key
>> that already has an assigned value is specified, that value will be
>> overwritten. "
>
> I was referring to the section just below headed:
> "Creating/modifying with square bracket syntax"
> but the paragraph that you mentioned certainly explains more of the
> actual details.

You know, that ("the maximum of the integer indices is taken and the new
key will be that value plus 1") is possibly a misleading clause.

For example:

$results = array();
$results[] = 'First One';

Reading that clause, "the maximum of the integer indices is taken and
the new key will be that value plus 1", one might expect the index of
"First One" to be 1. In fact, of course, it is 0. IOW, if there is no
index yet, then it doesn't add 1, but takes the first one as 0.

--
Shelly
Re: mysql_fetch_array [message #169797 is a reply to message #169790] Mon, 27 September 2010 07:15 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
sheldonlg wrote:
> On 9/26/2010 7:44 AM, Captain Paralytic wrote:
>> On Sep 26, 12:36 am, MikeB<mpbr...@gmail.com> wrote:
>>> Captain Paralytic wrote:
>>>> On Sep 25, 9:28 pm, MikeB<mpbr...@gmail.com> wrote:
>>>> > I have the following code:
>>> <snip>
>>>
>>>> > What PHP magic is at work here?
>>>
>>>> > Thanks.
>>>
>>>> I can recommend reading:
>>>> http://php.net/manual/en/language.types.array.php
>>>
>>> I've read that page before, but only now the following sentence makes
>>> sense:
>>>
>>> "If a key is not specified for a value, the maximum of the integer
>>> indices is taken and the new key will be that value plus 1. If a key
>>> that already has an assigned value is specified, that value will be
>>> overwritten. "
>>
>> I was referring to the section just below headed:
>> "Creating/modifying with square bracket syntax"
>> but the paragraph that you mentioned certainly explains more of the
>> actual details.
>
> You know, that ("the maximum of the integer indices is taken and the new
> key will be that value plus 1") is possibly a misleading clause.
>
> For example:
>
> $results = array();
> $results[] = 'First One';
>
> Reading that clause, "the maximum of the integer indices is taken and
> the new key will be that value plus 1", one might expect the index of
> "First One" to be 1. In fact, of course, it is 0. IOW, if there is no
> index yet, then it doesn't add 1, but takes the first one as 0.
>
Ah, but the maximum index value of a non existent array is therefore
obviously -1. :-)
Re: mysql_fetch_array [message #169798 is a reply to message #169790] Mon, 27 September 2010 07:44 Go to previous messageGo to next message
Hamish Campbell is currently offline  Hamish Campbell
Messages: 15
Registered: September 2010
Karma: 0
Junior Member
On Sep 27, 12:24 pm, sheldonlg <sheldonlg> wrote:
> On 9/26/2010 7:44 AM, Captain Paralytic wrote:
>
>
>
>
>
>> On Sep 26, 12:36 am, MikeB<mpbr...@gmail.com>  wrote:
>>> Captain Paralytic wrote:
>>>> On Sep 25, 9:28 pm, MikeB<mpbr...@gmail.com>    wrote:
>>>> > I have the following code:
>>>    <snip>
>
>>>> > What PHP magic is at work here?
>
>>>> > Thanks.
>
>>>> I can recommend reading:
>>>> http://php.net/manual/en/language.types.array.php
>
>>> I've read that page before, but only now the following sentence makes
>>> sense:
>
>>> "If a key is not specified for a value, the maximum of the integer
>>> indices is taken and the new key will be that value plus 1. If a key
>>> that already has an assigned value is specified, that value will be
>>> overwritten. "
>
>> I was referring to the section just below headed:
>> "Creating/modifying with square bracket syntax"
>> but the paragraph that you mentioned certainly explains more of the
>> actual details.
>
> You know, that ("the maximum of the integer indices is taken and the new
> key will be that value plus 1") is possibly a misleading clause.
>
> For example:
>
> $results = array();
> $results[] = 'First One';
>
> Reading that clause, "the maximum of the integer indices is taken and
> the new key will be that value plus 1", one might expect the index of
> "First One" to be 1.  In fact, of course, it is 0. IOW, if there is no
> index yet, then it doesn't add 1, but takes the first one as 0.
>
> --
> Shelly

Except that sharp readers will note that: "As mentioned above, if no
key is specified, the maximum of the existing integer indices is
taken, and the new key will be that maximum value plus 1. If no
integer indices exist yet, the key will be 0 (zero)."

Hamish
Re: mysql_fetch_array [message #169804 is a reply to message #169797] Mon, 27 September 2010 12:23 Go to previous messageGo to next message
sheldonlg is currently offline  sheldonlg
Messages: 166
Registered: September 2010
Karma: 0
Senior Member
On 9/27/2010 3:15 AM, The Natural Philosopher wrote:
> sheldonlg wrote:
>> On 9/26/2010 7:44 AM, Captain Paralytic wrote:
>>> On Sep 26, 12:36 am, MikeB<mpbr...@gmail.com> wrote:
>>>> Captain Paralytic wrote:
>>>> > On Sep 25, 9:28 pm, MikeB<mpbr...@gmail.com> wrote:
>>>> >> I have the following code:
>>>> <snip>
>>>>
>>>> >> What PHP magic is at work here?
>>>>
>>>> >> Thanks.
>>>>
>>>> > I can recommend reading:
>>>> > http://php.net/manual/en/language.types.array.php
>>>>
>>>> I've read that page before, but only now the following sentence makes
>>>> sense:
>>>>
>>>> "If a key is not specified for a value, the maximum of the integer
>>>> indices is taken and the new key will be that value plus 1. If a key
>>>> that already has an assigned value is specified, that value will be
>>>> overwritten. "
>>>
>>> I was referring to the section just below headed:
>>> "Creating/modifying with square bracket syntax"
>>> but the paragraph that you mentioned certainly explains more of the
>>> actual details.
>>
>> You know, that ("the maximum of the integer indices is taken and the
>> new key will be that value plus 1") is possibly a misleading clause.
>>
>> For example:
>>
>> $results = array();
>> $results[] = 'First One';
>>
>> Reading that clause, "the maximum of the integer indices is taken and
>> the new key will be that value plus 1", one might expect the index of
>> "First One" to be 1. In fact, of course, it is 0. IOW, if there is no
>> index yet, then it doesn't add 1, but takes the first one as 0.
>>
> Ah, but the maximum index value of a non existent array is therefore
> obviously -1. :-)
>

Try to get that from reading that sentence!

--
Shelly
Re: mysql_fetch_array [message #169806 is a reply to message #169768] Mon, 27 September 2010 12:37 Go to previous messageGo to next message
matt[1] is currently offline  matt[1]
Messages: 40
Registered: September 2010
Karma: 0
Member
On Sep 25, 4:28 pm, MikeB <mpbr...@gmail.com> wrote:
> I have the following code:
>
> $query = "SELECT * FROM classics";
> $result = mysql_query($query);
>
> if (!$result) die ("Database access failed: " . mysql_error());
> $rows = mysql_num_rows($result);
>
> for ($j = 0 ; $j < $rows ; ++$j)
> {
>         $results[] = mysql_fetch_array($result);
>
> }

This doesn't make much sense to me, unless you need the value of $rows
later on...

how about this instead:

if (!$result = mysql_query("..."))
die ("...");

while ($row = mysql_fetch_array($result))
$results[] = $row;

You're defining two additional variables that are totally unneeded.
Of course some pedant is probably going to jump down my throat for
putting assignments in my conditionals...
Re: mysql_fetch_array [message #169808 is a reply to message #169806] Mon, 27 September 2010 13:18 Go to previous messageGo to next message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
matt wrote:
> On Sep 25, 4:28 pm, MikeB<mpbr...@gmail.com> wrote:
>> I have the following code:
>>
>> $query = "SELECT * FROM classics";
>> $result = mysql_query($query);
>>
>> if (!$result) die ("Database access failed: " . mysql_error());
>> $rows = mysql_num_rows($result);
>>
>> for ($j = 0 ; $j< $rows ; ++$j)
>> {
>> $results[] = mysql_fetch_array($result);
>>
>> }
>
> This doesn't make much sense to me, unless you need the value of $rows
> later on...
>
> how about this instead:
>
> if (!$result = mysql_query("..."))
> die ("...");
>
> while ($row = mysql_fetch_array($result))
> $results[] = $row;
>
> You're defining two additional variables that are totally unneeded.
> Of course some pedant is probably going to jump down my throat for
> putting assignments in my conditionals...
>
How efficient is it to call the mysql_fetch_array($result) function
every time for the the loop evaluation vs.comparing with a variable
that is set once?
Re: mysql_fetch_array [message #169809 is a reply to message #169808] Mon, 27 September 2010 13:35 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On 27 Sep, 14:18, MikeB <mpbr...@gmail.com> wrote:
> matt wrote:
>> On Sep 25, 4:28 pm, MikeB<mpbr...@gmail.com>  wrote:
>>> I have the following code:
>
>>> $query = "SELECT * FROM classics";
>>> $result = mysql_query($query);
>
>>> if (!$result) die ("Database access failed: " . mysql_error());
>>> $rows = mysql_num_rows($result);
>
>>> for ($j = 0 ; $j<  $rows ; ++$j)
>>> {
>>>          $results[] = mysql_fetch_array($result);
>
>>> }
>
>> This doesn't make much sense to me, unless you need the value of $rows
>> later on...
>
>> how about this instead:
>
>> if (!$result = mysql_query("..."))
>>    die ("...");
>
>> while ($row = mysql_fetch_array($result))
>>    $results[] = $row;
>
>> You're defining two additional variables that are totally unneeded.
>> Of course some pedant is probably going to jump down my throat for
>> putting assignments in my conditionals...
>
> How efficient is it to call the mysql_fetch_array($result) function
> every time for the the loop  evaluation vs.comparing  with a variable
> that is set once?

You have me completely confused now!!!

Surely:
> for ($j = 0 ; $j < $rows ; ++$j)
> {
> $results[] = mysql_fetch_array($result);
> }
also calls mysql_fetch_array($result) just as many times as the other
one (OK one less, but you make up for that by calling
mysql_num_rows($result)).

Since in order to use the contents of $results[] you have to execute
another loop, I'm sure you can work out which is the more efficient.
Re: mysql_fetch_array [message #169811 is a reply to message #169809] Mon, 27 September 2010 14:12 Go to previous message
MikeB is currently offline  MikeB
Messages: 65
Registered: September 2010
Karma: 0
Member
Captain Paralytic wrote:
> On 27 Sep, 14:18, MikeB<mpbr...@gmail.com> wrote:
>> matt wrote:

>>> You're defining two additional variables that are totally unneeded.
>>> Of course some pedant is probably going to jump down my throat for
>>> putting assignments in my conditionals...
>>
>> How efficient is it to call the mysql_fetch_array($result) function
>> every time for the the loop evaluation vs.comparing with a variable
>> that is set once?
>
> You have me completely confused now!!!
>
> Surely:
>> for ($j = 0 ; $j< $rows ; ++$j)
>> {
>> $results[] = mysql_fetch_array($result);
>> }
> also calls mysql_fetch_array($result) just as many times as the other
> one (OK one less, but you make up for that by calling
> mysql_num_rows($result)).
>
> Since in order to use the contents of $results[] you have to execute
> another loop, I'm sure you can work out which is the more efficient.


D'oh. I see. Sorry.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Group sort syntax?
Next Topic: Code to create a cookie in PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 09:43:16 GMT 2024

Total time taken to generate the page: 0.02463 seconds