How to Use LOOP to get All the data from tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

How to Use LOOP to get All the data from tables

Post 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?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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']
...
nishmgopal
Forum Contributor
Posts: 101
Joined: Tue Mar 03, 2009 9:38 am

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

Post 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.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post 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.
Post Reply