PHP MySQL有两个查询
I need to do a SELECT * FROM table_X
, the problem is table_X is the result of another query, I don't know how to do it, perhaps with two loop, something like this :
<?php
$query1 = mysql_query("SELECT * FROM table_ref");
while ($row = mysql_fetch_array($query1))
{
$name = $row['table_name'];
$query2 = mysql_query(" SELECT * FROM '$name' ");
while ($row = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
}
?>
The tables are all similar & I can't do joint there's no keys. So what I want is to show only the results of the second query from each results of the first query !
我需要做一个 这些表格类似于&amp; 我不能联合那里没有钥匙。 所以我想要的是只显示第一个查询的每个结果中第二个查询的结果! p>
div> SELECT * FROM table_X code>,问题是table_X是结果 另一个查询,我不知道怎么做,也许有两个循环,如下所示: p>
&lt;?php
$ query1 = mysql_query( “SELECT * FROM table_ref”);
while($ row = mysql_fetch_array($ query1))
{
$ name = $ row ['table_name'];
$ query2 = mysql_query(“SELECT * FROM'$ name'“);
while($ row = mysql_fetch_array($ query2))
{
$ time = $ data ['itime'];
echo $ time;
}
}
?&gt;
code> pre>
So, what's your structure? I don't understand. You have column table_name where are listed a lot of tables? If so, just use backquotes on your $name:
$query2 = mysql_query(" SELECT * FROM `$name` ");
just change your quotes to have the query ready to be started
change
$query2 = mysql_query(" SELECT * FROM '$name' ");
to
$query2 = mysql_query(" SELECT * FROM `".$name."` ");
i would also rather sugest to check this part
while ($row = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
you already used variable $row
to fetching previus query so better to change to something else, look like $data
is matching your needs because you already used but you did not declare it
while ($data = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
Apart from the obvious that has been pointed out in the comments, you're overwriting $row
in the second loop.
Also, you're trying to read an array ($data
) that is not defined.
The following will work much better (but still isn't ideal):
$query1 = mysql_query("SELECT `table_name` FROM `table_ref`");
while ($row = mysql_fetch_array($query1))
{
$name = $row['table_name'];
$query2 = mysql_query("SELECT `itime` FROM `$name`");
while ($data = mysql_fetch_array($query2))
{
$time = $data['itime'];
echo $time;
}
}
Try this query:
select x.* from ( SELECT table_name FROM table_ref) as x