2009-10-31 66 views
0

根据我在StackOverflow上给出的建议,我尝试了下面的查询,但没有奏效。我试图让数据库中的“地盘”,25最近添加值的列表,无论他们是在什么表下面的代码提供以下错误:Double While while not working

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in domain.php on line 82

线82 while ($rowa = mysql_fetch_array($indexa))

任何想法为什么它不工作?

echo "<table class=\"samples\">"; 
$index = mysql_query("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='sitefeather'"); 
while ($row = mysql_fetch_array($index)) 
{ 
    $indexa = mysql_query("select site FROM index order by createdatetime desc limit 25"); 
    while ($rowa = mysql_fetch_array($indexa)) 
    {  
     echo '<tr><td><a href="sitelookup3.php?entry='.urlencode($rowa['site']).'&searching=yes&search=search">'.$rowa['site'].'</a></td></tr>'; 
    }  
} 
echo "</table>"; 
+0

我收到一个错误,当我执行“select site FROM index order by createdatetime desc limit 25”时,它说表格索引一定不能用这种方式写。我想知道你为什么要运行与外部查询无关的内部查询。 – 2009-10-31 06:14:36

+1

你会得到这个错误,因为index是mysql中的一个保留字,所以如果你打算将它用作表名,它必须被转义(在你的情况下,mysql正试图将它解释为它是一个真实的关键词)。 – shylent 2009-10-31 06:25:55

+0

供参考:你使用的是一个* nested * while循环。我还建议你阅读代码缩进。 – Artelius 2009-10-31 06:38:46

回答

2

你可能想要一个变量有适当的index。也许这个?

$indexa = mysql_query("select site FROM {$row['TABLE_NAME']} order by createdatetime desc limit 25"); 

但是,嗯......你在做什么?我不知道你到底想要完成什么,但是我的脑袋里响起了非常响亮的警铃。在查询中使用动态表名是一个重要的红旗,是数据库设计不佳的标志。

My database has a variable number of tables, all with the same structure.

这很糟糕。

这些表格是什么?让我们帮助您将所有这些数据整合到一张表中。

最简单的方法是创建一个带有额外列的单个表格,其中包含当前存储每行的表的名称。而不是使用表“foo”,“bar”和“baz”创建一个包含“foo”,“bar”或“baz”作为字符串值的列的单个表。

+0

我也想过一样,但仍然没有意义,因为我相信他正在试图查询一些元表,它将有列createdatetime。 – 2009-10-31 06:15:31

+0

谢谢...仍然给我一个错误。我的数据库具有可变数量的表格,全部具有相同的结构。我想打印所有表中总共25个最近添加的行的列表。 – John 2009-10-31 06:41:57

+1

“我想打印所有表中总共25个最近添加的行的列表。” ......这正是为什么用相同的结构创建一堆表是一个坏主意。 – longneck 2009-10-31 18:46:28

0

你innerquery是不相关的与外部查询,你可能想试试这个

<?php 
echo "<table class=\"samples\">"; 
$index = mysql_query("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='jammulinks'"); 
while ($row = mysql_fetch_row($index)) 
{ 
$indexa = mysql_query("select site FROM $row[0] order by createdatetime desc limit 25");//assuming you have site and createddatetime column there. 
while ($rowa = mysql_fetch_array($indexa)) 
{ 

    echo '<tr><td><a href="sitelookup3.php?entry='.urlencode($rowa['site']).'&searching=yes&search=search">'.$rowa['site'].'</a></td></tr>'; 
} 

} 
echo "</table>"; 
?> 
1

查询

select site FROM index order by createdatetime desc limit 25 

不应该工作。 “索引”是保留字。

你想在那里使用$ row ['TABLE_NAME']吗?

$indexa = mysql_query("select site FROM " + $row['TABLE_NAME'] + " order by createdatetime desc limit 25"); 
0

我想你在这里要做的是使用第一个查询($ index)对返回的所有表名执行SELECT操作。在这种情况下,你应该做这样的事情:

echo "<table class=\"samples\">"; 
// Get a list of table names from the schema 
$index = mysql_query("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='sitefeather'"); 
while ($row = mysql_fetch_array($index)) 
{ 
    // For every table in the schema, select site from it 
    $indexa = mysql_query("select site FROM {$row['table_name']} order by createdatetime desc limit 25"); 
    while ($rowa = mysql_fetch_array($indexa)) 
    { 
     echo '<tr><td><a href="sitelookup3.php?entry='.urlencode($rowa['site']).'&searching=yes&search=search">'.$rowa['site'].'</a></td></tr>'; 
    } 

} 
echo "</table>"; 

我不知道这是否会错误,如果该网站列没有一些你查询表的存在,但要注意。

0

考虑到如果使用反引号或作为表的取消引用来转义令牌,则可以使用表名和列的保留字。

mysql> create table `index` (id int(11)); 
Query OK, 0 rows affected (0.06 sec) 

mysql> show tables like 'index'; 
+----------------------------+ 
| Tables_in_umbrella (index) | 
+----------------------------+ 
| index      | 
+----------------------------+ 
1 row in set (0.00 sec) 

mysql> select * from `index`; 
Empty set (0.00 sec)