homepage Welcome to WebmasterWorld Guest from 54.161.246.212
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Inserting data into an array during while
otem

5+ Year Member



 
Msg#: 3350884 posted 11:30 pm on May 26, 2007 (gmt 0)

Hello, I'm grabbing content out of a database table, and I'm trying to input it into an array.

Right now, my code will output five records onto the screen. What I would like instead would be to output this content into one array.

How might I do that?

Thanks

$query=mysql_query("SELECT content FROM database LIMIT 5");
while($row=mysql_fetch_array($query)) echo $row["content"];

 

cameraman

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3350884 posted 1:06 am on May 27, 2007 (gmt 0)

while($row=mysql_fetch_array($query)) {
$data[] = $row['content'];
echo $row["content"];
}

otem

5+ Year Member



 
Msg#: 3350884 posted 3:41 am on May 27, 2007 (gmt 0)

Thanks for the reply!

I also found that the following script works too!

$array=array();
$query=mysql_query("SELECT content FROM database LIMIT 5");
while($row=mysql_fetch_array($query)) array_push($array,$row["content"]);

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3350884 posted 4:39 am on May 28, 2007 (gmt 0)

Just a random tip for the day...

I have observed that using this syntax:

$array[] = $row['content'];

is about twice as fast (on average) than using this:

array_push($array,$row["content"]);

when dealing with large arrays. :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved