【发布时间】:2014-05-20 14:42:23
【问题描述】:
我有一个联合表查询,但不断收到如下错误:
我也重新创建了数据库,但仍然出现同样的错误。检查了我与 mysql 的连接并且工作正常 - 我的联合表查询有什么问题吗?
$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";
}
这是我的连接
ob_start();
error_reporting(E_ALL);
ini_set( 'display_errors','1');
$user_name = "root";
$password = "root";
$database = "quotes";
$server = "localhost";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
echo "Working";
mysql_close($db_handle);
}
else {
print "Database NOT Found ";
}
这些是我遇到的错误
警告:mysql_query():无法建立到服务器的链接 在第 6 行的 /home/user/public_html/index.php 中
警告:mysql_fetch_array() 期望参数 1 是资源, 第 11 行 /home/user/public_html/index.php 中给出的布尔值
您能就此给我建议吗?
谢谢
【问题讨论】:
-
将错误报告添加到文件顶部
error_reporting(E_ALL); ini_set('display_errors', 1);您可能会收到“已弃用”错误消息。众所周知,最近会发生这种情况。你还没有向我们展示你是如何连接的。使用xxx替换凭据 -
还要确保您的数据库连接不是基于
mysqli_。mysqli_和mysql_函数不能混合使用。因此,请向我们展示您的完整代码,包括数据库连接。 -
mysql 已弃用,您应该实现 mysqli 或 PDO
-
您正在使用
mysql_close($db_handle);关闭您的数据库(您告诉它,如果找到则关闭连接)删除它或在成功查询后放置它。 @user3482036 -
代替
$author_id = $row['authors.id'];尝试$author_id = $row['id'];并为其余部分做同样的事情,看看会有什么效果。
标签: php mysql connection jointable