准备好的报表不会返回结果

时间:2014-03-06 08:29:40

标签: php mysql sql

我试着用准备好的陈述来做所有的查询,但对我来说是新的,我有一些麻烦。这是第一个查询,并且不会从表中回显结果。这是我到目前为止所做的。可能是真正的新手问题但对我来说是全新的。

if(isset($_GET['joke_id'])){
            $joke_id = $_GET['joke_id'];

            $qry = $con->prepare("SELECT * FROM joke WHERE joke_cat = ?");
            $qry->bind_param('i', $joke_id);
            $qry->execute();
            $result = $qry->get_result();
            $result->fetch_array();

    $result = mysqli_query($con, $qry) or die("Query failed: " . mysqli_errno($con));*/
            $line = mysqli_fetch_array($result, MYSQL_BOTH);

            if (!$line) echo '';
            $previd = -1;
            $currid = $line[0];
            if (isset($_GET['id'])) {
                $previous_ids = array();
                do {
                    $previous_ids[] = $line[0];
                    $currid = $line[0];
                    if ($currid == $_GET['id']) break;
                    $previd = end($previous_ids);
                    $line = mysqli_fetch_array($result, MYSQL_BOTH);
                } while ($line);
            }
   if ($line) {
                echo "<div id=\"box\">";
                echo nl2br($line['text']) . "<br /><br />";
                echo "<div id=\"share\"><span class='st_facebook' displayText='Facebook'></span>
                <span class='st_twitter' displayText='Tweet'></span>
                <span class='st_googleplus' displayText='Google +'></span></div>";
                echo '<br /><br /><br />';
                echo "</div>";

            }
            else echo '<p>Empty category</p><br/>';

这是我以前使用的方法来尝试PDO,它没有任何问题。

qry = "SELECT * FROM joke WHERE joke_cat = '$joke_id'";
            $result = mysqli_query($con, $qry) or die("Query failed: " . mysqli_errno($con));

1 个答案:

答案 0 :(得分:1)

$_GET['joke_id']$_GET['joke_cat']已设置?

或尝试

$qry = $con->prepare("SELECT * FROM joke WHERE joke_cat =:joke_cat");
$qry->bindParam(':joke_cat', $_GET['joke_cat'], PDO::PARAM_STR);
$qry->execute();
$result = $qry->fetchAll();