Im just making this as a resource for people who do not know how to/can't remember how to make a loop for mysql data in PHP
READ THIS FIRST: Kae's Mysql and PHP Basics
This thread will show you how to utilize the while function in php loops to help with outputting mysql data.
1. How to setup the while loop for data processing
The two variables above are very important...
To get mysql data into a variable, it's basically the same as putting a number in a variable
Now we must initiate the while loop
Now if your wondering why we used a < symbol instead of a <= is because mysql row numbers start at 0 and increase by 1
Let's pretend we have 2 columns in our table
A nifty little function we will be using is called mysql_result()
Heres basically how it works
In this case we will have to use it twice, and your $rownumber will be $i
Also, before i forget If you don't already know, there is a nice shortcut for upping your $vars by 1, its $var++;
Now let's start the actual coding
First Let's get our data from our table and initiate the while loop
OK, Now that that is ready, lets get the data from the row's (specified by $i) data in firstname
This stores the rows firstname into a var to make it easier to manipulate
Now we get the second row:
There, we are getting both columns of each row
Now we use a simple method to echo the results onto the page
The output of this would be
And there ya go, that's how you use while loops to output mysql data
I hope this helped
READ THIS FIRST: Kae's Mysql and PHP Basics
This thread will show you how to utilize the while function in php loops to help with outputting mysql data.
1. How to setup the while loop for data processing
PHP:
$i=0; // This is what we will use to know where we are in our loop
$num = mysql_num_rows($mysqlresults); // Can be named whatever you want and is compared to $i to know where we are
To get mysql data into a variable, it's basically the same as putting a number in a variable
PHP:
$mysqlresults = mysql_query($query);
PHP:
while($i < $num){
// Our processing will go here
}
Let's pretend we have 2 columns in our table
- firstname
- lastname
- firstname lastname
- Glenn Quagmire
- Peter Griffin
- Joe Swanson
- Cleveland Brown
- Robo Chicken
A nifty little function we will be using is called mysql_result()
Heres basically how it works
PHP:
$variable = mysql_result($mysql_data,$rownumber,$columnname);
Also, before i forget If you don't already know, there is a nice shortcut for upping your $vars by 1, its $var++;
Now let's start the actual coding
First Let's get our data from our table and initiate the while loop
PHP:
$names = mysql_query("SELECT * FROM ".$prefix."tablename"); // As you should know this will get ALL rows from table '$prefix'tablename
$num = mysql_num_rows($names); // Get how many rows (5)
$i=0; // I don't put in spaces because... its a habit... and it technically takes more time to type
$allnames = ""; // This will hold all of the names when we are done
while($i < $num){
}
OK, Now that that is ready, lets get the data from the row's (specified by $i) data in firstname
PHP:
while($i < $num){
$firstname = mysql_result($names,$i,"firstname");
}
This stores the rows firstname into a var to make it easier to manipulate
Now we get the second row:
PHP:
while($i < $num){
$firstname = mysql_result($names,$i,"firstname");
$lastname = mysql_result($names,$i,"lastname");
}
There, we are getting both columns of each row
Now we use a simple method to echo the results onto the page
PHP:
while($i < $num){
$firstname = mysql_result($names,$i,"firstname");
$lastname = mysql_result($names,$i,"lastname");
echo "Hello, my name is ".$firsname." of the ".$lastname." family.<br>";
}
The output of this would be
Code:
Hello, my name is Glenn of the Quagmire family.
Hello, my name is Peter of the Griffin family.
Hello, my name is Joe of the Swanson family.
Hello, my name is Cleveland of the Brown family.
Hello, my name is Robo of the Chicken family.
And there ya go, that's how you use while loops to output mysql data
I hope this helped