Home  >  Article  >  Backend Development  >  mysql_query老是查不到数据

mysql_query老是查不到数据

WBOY
WBOYOriginal
2016-06-13 12:03:491479browse

mysql_query总是查不到数据

<br /><?php<br />$dbhost = "localhost";<br />$dbuser = "root";<br />$dbpassword = "";<br />$dbdatabase = "blogtastic";<br /><br />$config_blogname = "Funny old world";<br />$config_author = "Pengsc";<br /><br />$config_basedir = "htpp://127.0.0.1/blogtastic/";<br />?><br /><br /><?php<br />//require("config.php"); //include config.php<br />$db = mysql_connect($dbhost, $dbuser, $dbpassword);<br />if(!$db)<br />{<br />	die('Could not connect: ' . mysql_error());<br />}<br /><br />$db_selected = mysql_select_db($dbdatabase, $db);<br />if (!$db_selected)<br />{<br />	die ("Can not use $dbdatabase : " . mysql_error());<br />}<br />?><br /><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /><head><br /><title><?php echo $config_blogname; ?></title><br /><link rel="stylesheet" href="stylesheet.css" type="text/css" /><br /></head><br /><br /><body><br /><!--div居中--><br /><div id="header"><br /><h1><?php echo $config_blogname; ?></h1> <!--设置网页中的标题文字,被设置的文字将以黑体或粗体的方式显示在网页中--><br />[<a href="index.php">home</a>]  <!--<a></a>这个标签是HTML中超链接, 输出结果是[home]--><br /></div><br /><br /><div id="main"><br /><!--the other mark, in footer--><br /><br /><?php<br />$sq1 = "SELECT entries.*, categories.cat From entries, categories<br />	WHERE entries.cat_id = categories.id<br />	ORDER BY dateposted DESC<br />	LIMIT 1;";<br />mysql_query("set names 'utf8'"); //数据库输出编码 应该与你的数据库编码保持一致.南昌网站建设公司百恒网络PHP工程师建议用UTF-8 国际标准编码. <br />$result = mysql_query($sql, $db);//执行一条 MySQL 查询<br />if($result)<br />{<br />	echo "<p>ctergories and entries</p>";<br />	echo "<p>ctergories and entries</p>";<br />	echo "<p>ctergories and entries</p>";<br />	echo "<p>ctergories and entries</p>";<br />	echo "<p>ctergories and entries</p>";<br />	$row = mysql_fetch_assoc($result); //从结果集中取得一行作为关联数组<br />	<br />	print_r($row);<br />	<br />	echo "<h2><a href=‘viewentry.php?id=" . $row['id']<br />	. "'>" . $row['subject'] . "</a></h2><br />";<br />	echo "<i>In <a href='viewcat.php?id" .  $row['cat_id']<br />	. "‘>" . $row['cat'] . "</a> - Posted on " . date("D jS F Y g.iA", strtotime($row['dateposted'])) . "</i>";<br />	echo "<p>";<br />	echo nl2br($row['body']);<br />	echo "</p>";<br />}<br />else<br />{<br />	print_r($row);<br />	echo "no ctergories and entries";<br />}<br /><br />?><br /><br /></div><br /><br /><div id="footer"><br /><br /> &copy; <?php echo $config_author; ?><br /></div><br /><br /></body><br /></html><br />

现在问题是,我直接在数据库中粘贴那个$sql中的查询语句以正常查询,就是通过mysql_query()查不到任何结果,这是什么原因????
------解决方案--------------------
有无错误信息?

$result = mysql_query($sql, $db) or die(mysql_error());  这样写看看是否有报错信息。
------解决方案--------------------
SELECT entries.*, categories.cat From entries, categories
    WHERE entries.cat_id = categories.id
    ORDER BY dateposted DESC
    LIMIT 1

在phpmyadin中执行可以查询到吗?
------解决方案--------------------
$sq1 = "SELECT entries.*, categories.cat From entries, categories
    WHERE entries.cat_id = categories.id
    ORDER BY dateposted DESC
    LIMIT 1;";

去掉LIMIT 1后面那个分号。


------解决方案--------------------
$sq1 = "SELECT entries.*, categories.cat From entries, categories
    WHERE entries.cat_id = categories.id
    ORDER BY dateposted DESC
    LIMIT 1;";

多了;
------解决方案--------------------
先要确定SQL是不是正确连接,如果可以执行一条简单的查询语句看看数据库选择,表前缀是不是正确的,如果这些都是正确的,那就是SQL查询语句有问题
 
如果你是myphpadmin的话把查询条件复制进去执行下,就知道是不是语句错误了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Mysql 话语报错Next article:PHP第一课 容易了解PHP