-1

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!

6
  • 2
    Why don't you try it?
    – Grant
    Feb 8, 2014 at 12:41
  • I get: Object of class mysqli_result could not be converted to string Feb 8, 2014 at 12:42
  • Would "SELECT name FROM my_table" not do it?
    – Grant
    Feb 8, 2014 at 12:45
  • 1
    What are you trying to achieve?
    – Grant
    Feb 8, 2014 at 12:46
  • select name from (select * from my_table) as temp Feb 8, 2014 at 12:47

2 Answers 2

0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.