We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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?

2
Contributors
1
Reply
17 Minutes
Discussion Span
1 Year Ago
Last Updated
2
Views
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
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0600 seconds using 2.69MB