mysql_query总是查不到数据

码到功成 2014-07-01 10:52:55

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbdatabase = "blogtastic";

$config_blogname = "Funny old world";
$config_author = "Pengsc";

$config_basedir = "htpp://127.0.0.1/blogtastic/";
?>

<?php
//require("config.php"); //include config.php
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
if(!$db)
{
die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db($dbdatabase, $db);
if (!$db_selected)
{
die ("Can not use $dbdatabase : " . mysql_error());
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?php echo $config_blogname; ?></title>
<link rel="stylesheet" href="stylesheet.css" type="text/css" />
</head>

<body>
<!--div居中-->
<div id="header">
<h1><?php echo $config_blogname; ?></h1> <!--设置网页中的标题文字,被设置的文字将以黑体或粗体的方式显示在网页中-->
[<a href="index.php">home</a>] <!--<a></a>这个标签是HTML中超链接, 输出结果是[home]-->
</div>

<div id="main">
<!--the other mark, in footer-->

<?php
$sq1 = "SELECT entries.*, categories.cat From entries, categories
WHERE entries.cat_id = categories.id
ORDER BY dateposted DESC
LIMIT 1;";
mysql_query("set names 'utf8'"); //数据库输出编码 应该与你的数据库编码保持一致.南昌网站建设公司百恒网络PHP工程师建议用UTF-8 国际标准编码.
$result = mysql_query($sql, $db);//执行一条 MySQL 查询
if($result)
{
echo "<p>ctergories and entries</p>";
echo "<p>ctergories and entries</p>";
echo "<p>ctergories and entries</p>";
echo "<p>ctergories and entries</p>";
echo "<p>ctergories and entries</p>";
$row = mysql_fetch_assoc($result); //从结果集中取得一行作为关联数组

print_r($row);

echo "<h2><a href=‘viewentry.php?id=" . $row['id']
. "'>" . $row['subject'] . "</a></h2><br />";
echo "<i>In <a href='viewcat.php?id" . $row['cat_id']
. "‘>" . $row['cat'] . "</a> - Posted on " . date("D jS F Y g.iA", strtotime($row['dateposted'])) . "</i>";
echo "<p>";
echo nl2br($row['body']);
echo "</p>";
}
else
{
print_r($row);
echo "no ctergories and entries";
}

?>

</div>

<div id="footer">
<br /> © <?php echo $config_author; ?>
</div>

</body>
</html>

现在问题是,我直接在数据库中粘贴那个$sql中的查询语句以正常查询,就是通过mysql_query()查不到任何结果,这是什么原因????
...全文
537 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
码到功成 2014-07-03
  • 打赏
  • 举报
回复
各位,这个问题我已经解决了,其中两处的变量不一到处,$sq1 = "SELECT entries.*, categories.cat From entries, categories WHERE entries.cat_id = categories.id ORDER BY dateposted DESC LIMIT 1;"; 这个变量是$sq1, 最后一个搞成了数字1, 应该是小写的字母L
joebnb 2014-07-03
  • 打赏
  • 举报
回复
先要确定SQL是不是正确连接,如果可以执行一条简单的查询语句看看数据库选择,表前缀是不是正确的,如果这些都是正确的,那就是SQL查询语句有问题 如果你是myphpadmin的话把查询条件复制进去执行下,就知道是不是语句错误了
码到功成 2014-07-03
  • 打赏
  • 举报
回复
引用 1 楼 jordan102 的回复:
有无错误信息? $result = mysql_query($sql, $db) or die(mysql_error()); 这样写看看是否有报错信息。
没有报错信息, 什么错误也没出现
码无边 2014-07-02
  • 打赏
  • 举报
回复
$sq1 = "SELECT entries.*, categories.cat From entries, categories WHERE entries.cat_id = categories.id ORDER BY dateposted DESC LIMIT 1;"; 多了;
  • 打赏
  • 举报
回复
$sq1 = "SELECT entries.*, categories.cat From entries, categories WHERE entries.cat_id = categories.id ORDER BY dateposted DESC LIMIT 1;"; 去掉LIMIT 1后面那个分号。
傲雪星枫 2014-07-01
  • 打赏
  • 举报
回复
SELECT entries.*, categories.cat From entries, categories WHERE entries.cat_id = categories.id ORDER BY dateposted DESC LIMIT 1 在phpmyadin中执行可以查询到吗?
一起混吧 2014-07-01
  • 打赏
  • 举报
回复
有无错误信息? $result = mysql_query($sql, $db) or die(mysql_error()); 这样写看看是否有报错信息。

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧