2009-10-31 69 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)