如何选择数据库并使用与PHP PHP数据库的JOIN

时间:2012-07-11 07:48:02

标签: php mysql html

我正在尝试运行此功能,但无法使其工作,我做错了什么(输出说我的fetch数组arg无效)?

$link = mysql_connect('localhost', 'over_app', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}

$result = mysql_query("SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
die('Invalid query: ' . mysql_error());
}

while($row = mysql_fetch_array($result)) { echo "Your Pic ID: $row['PicID']"; }

注意 Pic表位于Pics数据库中,Seen表位于SeenPics数据库

1 个答案:

答案 0 :(得分:0)

mysql_query返回布尔值false,查询失败..执行此操作

$result = mysql_query("SELECT Pic.PicID FROM Pic LEFT JOIN Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) {
   echo "Your Pic ID: $row['PicID']"; 
}

请勿使用mysql_* See this comment

更新

现在我们已经掌握了完整的事实 - 即您尝试跨多个数据库进行连接...问题是您的查询不完整...试试这个:

SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID