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

PHP Server Side Scripting Forum

fetch array question
How do I process a result set?

5+ Year Member

Msg#: 4272528 posted 8:29 pm on Feb 25, 2011 (gmt 0)

Hi all
I new to php mysql.I am fetching an array from the
$result_set by $result=mysql_fetch_array($result_set); and having the data in $result variable.But i want to run a loop in $result variable to go through all the columns are there so i can echo all columns name and their value in page.How to do that?

thanks in advance.



5+ Year Member

Msg#: 4272528 posted 9:48 pm on Feb 25, 2011 (gmt 0)


PHP documentation on the MySQL extension includes an example that does exactly that. Also take a look at the "User Contributed Notes" underneath for an example including column names.




WebmasterWorld Senior Member 5+ Year Member

Msg#: 4272528 posted 8:33 am on Feb 26, 2011 (gmt 0)


$sql = "SELECT * FROM `YourTable`";

$getyourdata = mysql_query($sql, $connectionHandle) or die(mysql_error());

//check to see if there is anything in your table first
if (mysql_num_rows($getyourdata) > 0){
//display your data here

while($result = mysql_fetch_array($getyourdata)){
//loop through data and display as you wish - this is just an example
echo $result['FirstColumn']." ".$result['SecondColumn']." ".$result['ThirdColumn']."\n\r";

//display a suitable error
echo "No results returned";


There are a few ways of doing this type of thing, but using fetch_array returns your data in both formats: "mysql_fetch_array Fetch a result row as an associative array, a numeric array, or both" <- quote from the manual.

I prefer to use fetch_object() as this only returns the data as the column names, so essentially this *should* mean less overhead when retrieving data from the database as there is no duplication of data - though people may argue the toss there.

WRT displaying in HTML, you can either use block echo's (difficult to maintain/edit) or you can drop in and out of php, which may look untidy, but is A LOT easier to maintain, and you don't need to escape chars to get the page displayed correctly either, but if your not concerned about W3C validation, that up to you.

There is another way of displaying blocks of html without dipping in and out, called 'here documnet', check out this [php.net] to get a better understanding of it (about a 3rd of the way down the page).

Any issues, post a message, and the good people of the forum will try to help.

Have fun, and enjoy your coding/programming.


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