1.11M Members

Setting while query to variable... possible?

 
0
 

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?

 
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'];
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: