1,105,320 Community Members

Setting while query to variable... possible?

Member Avatar
RazorRamon
Junior Poster in Training
79 posts since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
pritaeas
mod_pritaeas
11,289 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,832 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: