当我使用此查询时,我会收到数组中变量的通知错误 - 如果我使用 $author_slug = $row['slug'];而不是 $author_slug = $row['authors.slug'];它有效,但我怎么知道哪些数据来自哪个表?
$select_quote=mysql_query("SELECT authors.name, authors.id, authors.img, authors.slug, quotes.author_id, quotes.title, quotes.id, quotes.meta_keys, quotes.meta_description, quotes.slug, quotes.content
from quotes, authors
WHERE quotes.author_id = authors.id
ORDER BY RAND() LIMIT 1 ");
while ($row=mysql_fetch_array($select_quote)) {
$author_id = $row['authors.id'];
$author_name = $row['authors.name'];
$author_slug = $row['authors.slug'];
echo" $author_slug";
}
最佳答案
您可以为此字段使用别名:
SELECT authors.name
, authors.id
, authors.img
, authors.slug AS 'authors.slug'
, quotes.author_id
, quotes.title
, quotes.id
, quotes.meta_keys
, quotes.meta_description
, quotes.slug AS 'quotes.slug'
, quotes.content
FROM quotes, authors
WHERE quotes.author_id = authors.id
ORDER BY RAND() LIMIT 1
因此您可以在 php 中访问检索到的数据,如下所示:
$author_slug = $row['authors.slug']
$quotes_slug = $row['quotes.slug']
关于php - mysql PHP 中连接表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23763795/