mysql_fetch_array [message #169768] |
Sat, 25 September 2010 20:28  |
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   |
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 #169790 is a reply to message #169774] |
Sun, 26 September 2010 23:24   |
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   |
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   |
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   |
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 #169811 is a reply to message #169809] |
Mon, 27 September 2010 14:12  |
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.
|
|
|