![](/img/trans.png)
[英]Query MySQL results using PHP and multiple HTML dropdown boxes from multiple tables including 'id' reference tables
[英]show results from multiple mysql tables using php
我有多個具有相同結構的mysql表。 我試圖用一個查詢顯示所有表的所有結果。 我已經在網上搜索了幾個小時,這就是我想出的。
$videos_query = mysql_query("SELECT
table1.title,
table1.url,
table1.image,
table2.title,
table2.url,
table2.image,
table3.title,
table3.url,
table3.image
FROM
table1
INNER JOIN table2 ON table1.url = table2.url
INNER JOIN table3 ON table1.url = table3.url");
while($video_info = mysql_fetch_array($videos_query)){
但是,在PHP im中使用以上代碼會得到錯誤“警告:mysql_fetch_array()期望參數1為資源,給定布爾值”
您的SQL查詢中有一個錯誤table1
必須是table2
:
$videos_query = mysql_query("SELECT
table1.title,
table1.url,
table1.image,
table2.title,
table2.url,
table2.image,
table3.title,
table3.url,
table3.image
FROM
table1
INNER JOIN table2 ON table1.url = table2.url
INNER JOIN table3 ON table1.url = table3.url");
if (mysql_num_rows($videos_query) > 0) {
while ($video_info = mysql_fetch_array($videos_query)) {
// process your data here
}
}
如果要執行聯合選擇:
$videos_query = mysql_query("
SELECT title, url, image
FROM table1
UNION
SELECT title, url, image
FROM table2
UNION
SELECT title, url, image
FROM table3
");
if (mysql_num_rows($videos_query) > 0) {
while ($video_info = mysql_fetch_array($videos_query)) {
// process your data here
}
}
另外,也許您想按某列排序-因此,請在此添加ORDER BY
+ column_name。
更改
INNER JOIN table2 ON table1.url = table1.url
至
INNER JOIN table2 ON table1.url = table2.url
在選中“ ON
您正在比較table1.url = table1.url
,這是不正確的。
這是正確的代碼:
$videos_query = mysql_query("SELECT
table1.title,
table1.url,
table1.image,
table2.title,
table2.url,
table2.image,
table3.title,
table3.url,
table3.image
FROM
table1
INNER JOIN table2 ON table1.url = table2.url
INNER JOIN table3 ON table1.url = table3.url");
while($video_info = mysql_fetch_array($videos_query)){
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.