Double While循环不起作用

时间:2009-10-31 06:02:41

标签: php mysql

根据我在StackOverflow上给出的建议,我尝试了下面的查询,但它没有用。我正在尝试获取数据库中“site”最近添加的25个值的列表,无论它们在哪个表中。下面的代码给出以下错误:

  

警告:mysql_fetch_array():提供的参数不是第82行的domain.php中的有效MySQL结果资源

第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>";

5 个答案:

答案 0 :(得分:2)

你可能想要一个变量代替index。也许这个?

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

然而,嗯......你在做什么?我不知道你到底想要完成什么,但是脑子里响起了很响的警钟。在查询中使用动态表名称是一个主要的红旗,并且表明数据库设计不佳。

  

我的数据库有可变数量的表,所有表都具有相同的结构。

那很糟糕。

这些表格中有什么?让我们帮助您将所有这些数据放入一个表格中。

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

答案 1 :(得分:1)

查询

select site FROM index order by createdatetime desc limit 25

不应该工作。 “index”是一个保留字。

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

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

答案 2 :(得分:0)

您的内部查询与外部查询无关,您可能想尝试这个

<?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>";
?>

答案 3 :(得分: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>";

如果您查询的某些表中不存在网站列,我不确定是否会出错,但请注意。

答案 4 :(得分: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)
相关问题