我正在尝试将我的类别从mysql数据库中显示出来,并通过php include将其添加到我的索引页面,这里是类别页面,虽然这是我收到错误但不确定为什么但是?
<?php //which category are we showing?
$category_id = $_GET['catid']; ?>
<h2>All Categories</h2>
<?php
//get 2 latest published posts
$query_latest = "SELECT title, body, date, category_id, post_id
FROM posts
WHERE is_published = 1
AND category_id = $category_id
ORDER BY date DESC";
//run the query code on the DB
$result_latest = mysql_query( $query_latest );
//loop it, work with one post at a time
while( $row_latest = mysql_fetch_array( $result_latest ) ){ ERROR ROW 19
?>
<div class="post">
<h2><?php echo $row_latest['title'] ?></h2>
<h3>Posted on <?php echo $row_latest['date']; ?> in the category of something</h3>
<p><?php echo $row_latest['body'] ?></p>
</div>
<?php } //end while loop ?>
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given
in C:\xampp\htdocs\schellshockdesign.com\term5final\finalproject\category.php
on line 19
答案 0 :(得分:1)
您未连接到数据库。
此外,您应该使用mysql_fetch_assoc()而不是mysql_fetch_array()。
此外,您应该对查询中使用的变量使用mysql_real_escape_string。
答案 1 :(得分:0)
这应该对你有用
<h2>All Categories</h2>
<?php
$category_id = htmlspecialchars($_REQUEST["catid"]);
$category_id = preg_replace("/[^0-9]/","", $category_id);
//get 2 latest published posts
$SQL = "SELECT * FROM posts WHERE is_published = '1' AND category_id = '$category_id' ORDER BY date DESC LIMIT 2";
$result = mysql_query( $SQL );
while( $row = mysql_fetch_array( $result ) ) {
$category_id = $row["category_id"];
$post_id = $row["post_id"];
$title = $row["title"];
$body = $row["body"];
$date = $row["date"];
echo '<div class="post">
<h2>'.$title.'</h2>
<h3>Posted on '.$date.' in the category of something</h3>
<p>'.$body.'</p>
</div>';
}
?>
答案 2 :(得分:0)
请尝试以下步骤。可能是您可以使用以下内容进行调试。
回显查询并尝试在phpadmin执行。如果它在那里运行,问题在于数据库连接。
如果查询没有运行,那么它清楚地表明问题出在查询..类似字段不正确,错误的表名,错误的数据输入许多其他。
由于