I have 5 lines data in my database. I want each line of it in different variable. How can it be done?

Plz help me with some code.

Recommended Answers

All 3 Replies

You need to supply us with a little bit more information.

What 5 lines of data?
What are the field name(s)?

Try something like this:

$result=mysql_query('SELECT * FROM `tablename`');
for ($n=1;$row=mysql_fetch_array($result);$n++) {
    $var='variable'.$n;
    $$var=$row['columnname']; //replace columnname with real column name
    }
echo $variable1;
echo '<br>';
echo $variable2;
echo '<br>';
echo $variable3;
echo '<br>';
echo $variable4;
echo '<br>';
echo $variable5;

you can do as

$result=mysql_query('SELECT * FROM `tablename`');
$row=mysql_fetch_array($result);
$count=mysql_num_rows($row);
for ($n=1; $n<=$count; $n++) 
{
   $variable="myvariable".$n;
   echo $variable=$row['field']."<br>";
}

the variable name will be

myvariable1
myvariable2
myvariable3
myvariable4
myvariable5

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.