0

我正在尝试从 mysql 数据库中获取我的类别以显示,并通过 php 包含将其添加到我的索引页面,这是类别页面虽然这是我收到错误的地方,但不知道为什么?

<?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
4

3 回答 3

1

您没有连接到数据库。

此外,您应该使用 mysql_fetch_assoc() 而不是 mysql_fetch_array()。

此外,您应该在查询中使用的变量上使用 mysql_real_escape_string。

于 2012-07-08T02:34:01.167 回答
0

请尝试以下步骤。可能您可以使用以下方法对其进行调试。

  1. 回显查询并尝试在 phpadmin 执行。如果它在那里运行,则问题出在数据库连接上。

  2. 如果查询没有运行,那么它清楚地表明问题出在查询 ..like 字段不正确、错误的表名、错误的数据输入等等。

谢谢

于 2012-07-08T06:37:18.443 回答
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>';
    }
    ?>
于 2012-07-08T05:09:40.843 回答