Setting while query to variable... possible?
Ok i have a lot of tables that are created when a user inputs information. Some info gets stored in a "Main" table while the majority is saved in its own self created table. I do not know the number of "topics" in Main table. so in way i'm trying to make a double query but i'm not sure if i'm saving the $all variable correctly.
$sql= "select * from main";
$result=mysql_query($sql);
$counter=mysql_num_rows($sql);
$i="1";
$all= while($row=mysql_fetch_array($result)){;
$all.= echo ”Select * From ". ($row[‘topic’]);
$all.= if($i <$counter){;
$all.= echo “ UNION ALL “;
$all.= }else{;
$all.= Echo “ ORDER BY id DESC ”;
$all.= }};
$query=mysql_query($all);
while($row=mysql_fetch_array($query)){
echo $row[‘name’] $row[‘age’] $row[‘city’] $row[‘state’] ;
}
I keep getting Unexpected T_While and Unexpected T_Variables in the lines where the while starts and where $row is used. I've done research and tutorials say that those errors are usually due to a missing ; but i dunno i'm stuck... Any suggestions on my $all variable?
17 Minutes
Discussion Span
Related Article: mysql php query
is a solved PHP discussion thread by patraghost that has 6 replies and was last updated 2 years ago.
RazorRamon
Junior Poster in Training
78 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Your syntax is way off:
$sql = 'select * from main';
$result = mysql_query($sql);
$counter = mysql_num_rows($sql);
$i = 1;
while ($row = mysql_fetch_array($result))
{
$all .= 'Select * From ' . $row['topic'];
if ($i < $counter)
{
$all .= ' UNION ALL ';
}
else
{
$all .= ' ORDER BY id DESC ';
}
}
$query = mysql_query($all);
while ($row = mysql_fetch_array($query))
{
echo $row['name'] . $row['age'] . $row['city'] . $row['state'];
}
pritaeas
Posting Prodigy
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98