Forum Moderators: coopster

Message Too Old, No Replies

Simplify retrieval of 100 variables from database

Currently writing 100 individual variables

         

s9901470

2:16 pm on Feb 27, 2006 (gmt 0)

10+ Year Member



Hi

I'm retrieving large numbers of variables (100) from a database using PHP/MySQL. At the moment I list all 100 variabels as per below. Is there a way to simplify this piece of code?

$result = $connector->query("SELECT * FROM table1 WHERE username='$username' ");
while ($row = $connector->fetchArray($result)){
$q1=$row['q1'];
$q2 = $row['q2'];
$q3=$row['q3'];
$q4=$row['q4'];
$q5=$row['q5'];
$q6=$row['q6'];
$q7=$row['q7'];
$q8=$row['q8'];
$q9=$row['q9'];
...
$q100=$row['q100'];

Any suggestions most welcome!
Ed

omoutop

2:25 pm on Feb 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



try something like this :

for ($i=1;$i<=100;$i++)
{

// connection and sql execution
// while $row=....
{

$my_q = "q".$i; // this creates the q1, q2, q3, etc
$q_value = $row[$my_q]; // value of field q(x) from db

// do something with $my_q and $q_value

} // close while

}