Page 1 of 1

How to Use LOOP to get All the data from tables

Posted: Wed Mar 04, 2009 7:00 am
by nishmgopal
I dont know whether I am being completely stupid or what, but I cant seem to pick out all the records from my database.

The overall problem is:

I have a table with n amount of rows, and would like to pull all these rows out and store them in an array in php.

What I have so far is:

myquery=SELECT * FROM Table1;
result=mysql_fetch_array(myquery){

$skill1=$row['skill1']
$skill2=$row['skill2']
.....

this will store skill1 and skill2, but what if i have n skills? I have tried using the loop but im not sure if i did it correctly because I still had to state the variables in my array. If i have 8 variables, 8 skills will show and so on.

Can anyone help?

Re: How to Use LOOP to get All the data from tables

Posted: Wed Mar 04, 2009 7:34 am
by Mark Baker
If you have columns called skill1, skill2, etc in your table, then n must be fixed anyway.... and it means that you haven't normalised your tables properly.

However, to answer your query about storing in an array

$skills = array();

$skills[]=$row['skill1']
$skills[]=$row['skill2']
...

Re: How to Use LOOP to get All the data from tables

Posted: Wed Mar 04, 2009 7:37 am
by nishmgopal
Thank you, but what values goes into the square brackets:

$skill[]=$row['skill1];

sorry if it is a dumb question, but i am quiet new to programmin.

Re: How to Use LOOP to get All the data from tables

Posted: Wed Mar 04, 2009 11:40 am
by Mark Baker
nishmgopal wrote:Thank you, but what values goes into the square brackets:
No value goes in the square brackets.
This is basically telling PHP to create a new element at the end of the current array, and populate it with the appropriate value.