Can I do this?
$query = "SELECT * FROM my_table";
$results = mysqli_query($db, $query)
$query = "SELECT name FROM $results";
$results = mysqli_query($db, $query)
Any help much appreciated!
Can I do this?
$query = "SELECT * FROM my_table";
$results = mysqli_query($db, $query)
$query = "SELECT name FROM $results";
$results = mysqli_query($db, $query)
Any help much appreciated!
No. For really complex queries you can use temporary tables:
CREATE TEMPORARY TABLE tmp SELECT * FROM my_table;
SELECT name FROM tmp;
But it causes big overhead for mysql server.
In your query, you are taking the results from this:
$query = "SELECT * FROM my_table";
And then trying to do this:
$query = "SELECT name FROM $results";
I can think of two things that you really want to do. The first is trivial:
$query = "SELECT name FROM my_table";
The second is that you want to get the table name from the first query and use it in the second. You can do that, assuming that the first query is returning one row and one columns. Or that the second is inside a loop and you have just oversimplified the code.
You can do this because you are constructing the strings that form the queries through the application. You cannot do this in basic SQL, because table names and column names cannot be dynamically created. They can be using a prepare
statement, which is equivalent to constructing the strings in php.