Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

fetch array question

How do I process a result set?

8:29 pm on Feb 25, 2011 (gmt 0)

New User

5+ Year Member

joined:Oct 13, 2009
votes: 0

Hi all
I new to php mysql.I am fetching an array from the
and having the data in
variable.But i want to run a loop in
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.
9:48 pm on Feb 25, 2011 (gmt 0)

Preferred Member

10+ Year Member

joined:Feb 11, 2006
votes: 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.


8:33 am on Feb 26, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
votes: 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.