Jump to content

SELECT * FROM $variable instead of the tablename possible?


Bentley4

Recommended Posts

Hi guys, the following code gives the parsing error "unexpected T_VARIABLE":

<?php
$con = mysql_connect("localhost","XXX", "YYY");
if (!$con) {die('Could not connect: ' . mysql_error("oop"));}
mysql_select_db("XXX_testDB") or die(mysql_error());
  
$counter = 1;
$Tcounter = 1;
$Tresult = mysql_query("SELECT * FROM NameTable WHERE id = $Tcounter");
$Trow = mysql_fetch_array($Tresult);    
  
$result = mysql_query("SELECT * FROM "$Trow['NameOfTable']" WHERE id = $counter");
$row = mysql_fetch_array($result);
echo $row['TQuestion'];  
?>

 

So I have a Table in Mysql that is called 'NameTable'. From that table I am drawing an element in the 1st row under the column of NameOfTable. I want to call another element of another table through the table I described using a variable. Like this:

$result = mysql_query("SELECT * FROM "$Trow['NameOfTable']" WHERE id = $counter");

 

"$Trow['NameOfTable']": I also tried using single quotes or without and it doesn't work either.

What is wrong with this?

Link to comment
Share on other sites

 $result = mysql_query("SELECT * FROM {$Trow['NameOfTable']} WHERE id = $counter");

 

However, you need to rethink your design. You should NOT make a new table for each different piece of data, where you must execute one query JUST to get the table name that you are going to actually query.

Link to comment
Share on other sites

Dear PFMaBiSmAd,

 

1. Thanks for your response.

2. It still doesn't work with curly brackets either. Any clue why?

3. I'm trying to loop through different tables, completely automise it. I don't see any other way.

btw, the full code is longer though, I just chopped a bit out of it for brevity. Which other code do you suggest to have the same functionality?

Here is the full code as a reference:

<?PHP  
   echo "<h2>Exercises</h2>";
   $name = $_GET['sid'];
   $timein = time();
   $quest=1;
   $nextquest=2;

   $con = mysql_connect("localhost","XXX", "YYY");
   if (!$con) {die('Could not connect: ' . mysql_error("oop"));}
   mysql_select_db("XXX_testDB") or die(mysql_error());
  
   if(!isSet($_GET['qandanumber']) && isSet($_GET['sid'])){
     $_GET['qandanumber'] = 1;
     $qanda = $_GET['qandanumber'];
     $nextqanda = $qanda+1;}  

   if(isSet($_GET['qandanumber'])){
     $qanda = $_GET['qandanumber'];
     $nextqanda = $qanda+1;}

   $totalresult = mysql_query("SELECT * FROM QANDATable");
   $num_rows = mysql_num_rows($totalresult);

   $counter = 1;
   $i = 1;
   $Tcounter = 1;

   while($counter <= $qanda){
     
     $Tresult = mysql_query("SELECT * FROM NameTable WHERE id = $Tcounter");
     $Trow = mysql_fetch_array($Tresult);
     $num_Trows = mysql_num_rows($Tresult);     

     if($counter < 2){
       $result = mysql_query("SELECT * FROM QANDATable WHERE id = $counter");
       $row = mysql_fetch_array($result);
       echo $row['TSuperQuestion'];
       echo $row['TQuestion'];}
     else{
       if($counter%2){
          $result = mysql_query("SELECT * FROM QANDATable WHERE id = $counter - $i");
          $row = mysql_fetch_array($result);
          echo $row['TQuestion'];
          $i++;}
       else {
          $result = mysql_query("SELECT * FROM QANDATable WHERE id = $counter - $i");
          $row = mysql_fetch_array($result);
          echo $row['TAnswer'];}} 
     if($counter/2 == $num_rows){       
       $Tcounter ++;
       }
   $counter++;}
   echo "<a href=\"/Code-sandbox.php?sid=".$name."&qandanumber=".$nextqanda."\">next</a> <br/>";
   } 
?>

 

So what I essentially want to do is when the last element of a table is echooed, start echo each element again from a next table. For those just tuning in, I want to replace 'QANDATable' by '$Trow['NameOfTable'];' .

This should be a looped so I don't see any other way than replacing the name of each table by a variable in the SELECT language construct. What do you think?

Link to comment
Share on other sites

well first of all. unless you defined it yourself, isSet is not a function. Perhaps you mean isset().

 

secondly, in your OP example, you aren't concatenating correctly. I'm assuming the page threw a "unexpected string" error. The example PFM gave you should theoretically work, although you could also do

 

$result = mysql_query("SELECT * FROM " . $Trow['NameOfTable'] . " WHERE id = $counter");

 

if you wanted to. Both are equivalent.

Link to comment
Share on other sites

2. - cannot help you with your code or any errors without seeing the code and the error.

 

The code you just posted, where you are executing the same query, but for different id values inside of a loop, is not using different table names, and so does not demonstrate what you state you are trying to do.

 

However, you should only execute ONE query that gets all the rows you want in the order that you want them, and then simply output the information the way you want it when you iterate over the rows that the single query returned.

 

Php function names are not cases-sensitive, isSet is isset

 

 

Link to comment
Share on other sites

 

secondly, in your OP example, you aren't concatenating correctly. I'm assuming the page threw a "unexpected string" error. The example PFM gave you should theoretically work, although you could also do

 

$result = mysql_query("SELECT * FROM " . $Trow['NameOfTable'] . " WHERE id = $counter");

 

if you wanted to. Both are equivalent.

 

Thanks, this one does work!!!!

does not demonstrate what you state you are trying to do.

Ok, I'm sorry. It works now though.

 

Php function names are not cases-sensitive, isSet is isset

 

Indeed, I changed it and the functionality doesn't change.

Link to comment
Share on other sites

What I don't understand though is why I don't need to escape the double quotes in:

$result = mysql_query("SELECT * FROM " . $Trow['NameOfTable'] . " WHERE id = $counter");

and

 $totalresult = mysql_query("SELECT * FROM ".$Trow['NameOfTable']."");

 

This works, but I don't understand why.

Link to comment
Share on other sites

I know it is. But before I used double quotes and I had to escape it, like in this example:

 

echo "<a href=\"/index.php?sid=".$name."&question=".$nextquest."\">next</a>";

 

It is not an answer unless you meant to say, concatenation quotes never need to be escaped.

Link to comment
Share on other sites

Quotes inside of a string need to be escaped (when they are the same type of quote that starts/ends the string.) The quotes that start and end the string aren't escaped.

 

In your last post above, the escaped double-quotes are inside of a double-quoted string. In your previous post where you asked about this, you don't have any quotes inside any of the double-quoted strings.

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.