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?

Dani AI

Generated

A few quick, practical things to fix and consider beyond the syntax fixes already suggested by so the dynamic UNION approach actually works and is safe.

First, the parse errors come from trying to assign a while to a variable and from using echo and bad argument names inside that assignment. Build the SQL string explicitly: initialize an empty string, loop over the topic rows and append with .=, increment your counter or use an index/flag, and do not use echo while building the string. Also call mysql_num_rows($result) (not $sql). Put the final ORDER BY after the whole UNION, and select explicit columns (not SELECT *) so every SELECT in the UNION returns the same columns.

Second, security and robustness: table identifiers cannot be bound with prepared statements, so validate table names rigorously (a whitelist or a strict regex like ^[A-Za-z0-9_]+$) or verify existence in information_schema.tables before including them. Never include unvalidated user input directly in identifiers. Prefer UNION ALL for performance when duplicates are not a concern.

Third, preferred approach: stop using deprecated mysql_* functions and either normalize your schema (single table with a topic column) or use PDO/mysqli and validate identifiers. Example pattern (PDO) — fetch topic names, allow only safe names, build the UNION from the validated list, execute and fetch results:

$pdo = new PDO(...);
$topics = $pdo->query('SELECT topic FROM main')->fetchAll(PDO::FETCH_COLUMN);
$tables = array_filter($topics, function($t){ return preg_match('/^[A-Za-z0-9_]+$/',$t); });
if ($tables) {
  $sql = implode(' UNION ALL SELECT name,age,city,state FROM ', array_map(function($t){ return "`$t`"; }, $tables));
  $sql = 'SELECT name,age,city,state FROM ' . $sql . ' ORDER BY id DESC';
  $rows = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}

Finally, consider restructuring: having one table per topic is fragile and leads to complex, slow queries. If redesigning is possible, move to a single table with a topic column and index it — queries become simpler, faster, and much safer.

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'];
}
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.