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

Home » Imported messages » comp.lang.php » Re: mysqli fetch_assoc() straight to array
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: mysqli fetch_assoc() straight to array [message #186023 is a reply to message #186022] Sat, 31 May 2014 17:21 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Sat, 31 May 2014 10:32:44 -0400, Lew Pitcher wrote:

> On Friday 30 May 2014 17:36, in comp.lang.php, "richard"
> <noreply(at)example(dot)com> wrote:
>
>> On Fri, 30 May 2014 16:25:10 -0400, Lew Pitcher wrote:
>>
>>> On Friday 30 May 2014 16:23, in comp.lang.php, "Lew Pitcher"
>>> <lew(dot)pitcher(at)digitalfreehold(dot)ca> wrote:
>>>
>>>
>>>> On Friday 30 May 2014 07:59, in comp.databases.mysql, "richard"
>>>> <noreply(at)example(dot)com> wrote:
>>>>
>>>>
>>>> > Is there a way to make it simpler to just load an array based upon
>>>> > the results of $row?
>>>> > Rather than having to assign each individual column one at a time?
>>>>
>>>> /This/ is the sort of posting that I was talking about earlier.
>>>>
>>>> Richard, this isn't a mysql issue. Mysql does not define how PHP
>>>> manages array assignments, PHP does.
>>>>
>>>> > $array=$row Versus $array[col1]=$row[col1]
>>>> > $array[col2]=$row[col2]
>>>> >
>>>> >
>>>> > while ($y<=69){
>>>> > while ($n<=40){
>>>> >
>>>> > if ($n<10){$n="0".$n;}
>>>> > $x=$y.$n;
>>>> > $sql = "SELECT * FROM top20 where songs='$x'";
>>>> > $result = $mysqli->query($sql);
>>>> > $row = $result->fetch_assoc();
>>>> >
>>>> > echo $n.") ".$row['songs']."<br>";
>>>> >
>>>> > $n++;
>>>> >
>>>> > }
>>>> > $n=1;
>>>> > $y++;
>>>> > }
>>>>
>>>> Followup set to comp.lang.php
>>>>
>>>> FWIW, PHP permits assignment of arrays, so
>>>> $row = $result->fetch_assoc(); $array = $row;
>>>> echo $n.") ".$array['songs']."<br>";
>>>> is legal PHP code.
>>>
>>> As is, FWIW
>>> $row = $result->fetch_assoc(); $array[$n] = $row;
>>> echo $n.") ".$array[$n]['songs']."<br>";
>>>
>>> Or even
>>> $row[$n] = $result->fetch_assoc();
>>> echo $n.") ".$row[$n]['songs']."<br>";
>>
>> I wasn't sure if that would work or not.
>
> All the above suggestions work.
> First off, the PHP language guarantees that they work, so they work in
> principle. Secondly, I've tried them all, and they work in practice
>
>> But I only have a few columns in the table anway so I just did the
>> assignments one item at a time.
>
> Your problem, your loss.

I think I have now parsed his original problem, although he still hasn't
stated it clearly.

I think that what he wants to do is retrieve an array containing all the
rows returned by the query in one go, instead of row by row.

Now this is where the smart cookie would read the php mysqli::result
class information to see what function they might want:

http://www.php.net/manual/en/class.mysqli-result.php

And there the really smart cookie might notice the function fetch_all

http://www.php.net/manual/en/mysqli-result.fetch-all.php

And it's possible that, depending on whether he wants the sub arrays
indexed by column number or column name, that what richard is actually
after is something like:

$sql = "select name from songs order by name";
$result = mysqli_query( $sql );
if ( $result !== false ) {
$data = mysqli_fetch_all( $result, MYSQLI_ASSOC );
}
else {
/* handle the error dumbass */
}

(where MYSQLI_ASSOC could also be MYSQLI_NUM or MYSQLI_BOTH)

which might then give him an array of arrays whose keys looked like:

$data[0]["name"]
$data[1]["name"]
$data[2]["name"]
......
$data[n]["name"]

But I'm still not sure if this is what he actually wants to do, it's just
my best guess. It's certainly not the obvious solution to the originally
posted question.

There's also a caution on using the fetch_all function. It's memory
hungry and can exceed your process memory limits! That's probably not
going to be a clean exit!

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: mysqli fetch_assoc() straight to array (-> comp.lang.php)
Next Topic: ArrayAccess interface, Traversable interface and foreach
Goto Forum:
  

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

Current Time: Mon Jun 17 12:40:55 GMT 2024

Total time taken to generate the page: 0.04900 seconds