如何浏览参数化查询的结果?

发布于 2025-01-31 10:03:42 字数 1775 浏览 4 评论 0原文

我正在尝试浏览PHP中的对神经分散查询的结果。 我正在尝试这种方式

//creating a prepared statement
        $stmt = $mysqli->stmt_init();
        $query = "SELECT pcode,price,description FROM products WHERE description like ? ORDER BY PRICE";
        $stmt->prepare($query);

        $search_criteria = '%'.$search_criteria.'%';                
        $stmt->bind_param('s', $search_criteria);
        $stmt->execute();   

        $i=0;
        while($stmt->fetch())
        {
            if ($i++%2 == 0) $trow = "even"; else $trow = "odd"; 
                echo "              <tr class='{$trow}'>";
                echo "                  <td>{$row['pcode']}</td><td>{$row['price']}</td><td>{$row['description']}</td>";
                echo "              </tr>";
                        
        }   

        if ($i==0) {
            echo "<tr class='even'><td colspan='3'><h3>No Results.</h3></td></tr>";
        }

,但是我的代码正在输入IF语句。 我该如何完成结果? 数据库是mySQL。

<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname) or die('Did you <a href="setupreset.php">setup/reset the DB</a>? <p><b>SQL Error:</b>' . mysql_error($conn) . '<p><b>SQL Statement:</b>' . $query);

function execute_query($the_query){
    global $conn;
    $result = mysql_query($the_query) or die('<font size=2><b>SQL Error:</b>' . mysql_error($conn) . '<br/><p><b>SQL Statement:</b>' . $the_query . '<br/>Did you run <i>setupreset.php</i> to setup/reset the DB?</font>');
    return $result;
}
?>

I'm trying to go through the result of a paremeterized query in PHP.
I'm trying this way

//creating a prepared statement
        $stmt = $mysqli->stmt_init();
        $query = "SELECT pcode,price,description FROM products WHERE description like ? ORDER BY PRICE";
        $stmt->prepare($query);

        $search_criteria = '%'.$search_criteria.'%';                
        $stmt->bind_param('s', $search_criteria);
        $stmt->execute();   

        $i=0;
        while($stmt->fetch())
        {
            if ($i++%2 == 0) $trow = "even"; else $trow = "odd"; 
                echo "              <tr class='{$trow}'>";
                echo "                  <td>{$row['pcode']}</td><td>{$row['price']}</td><td>{$row['description']}</td>";
                echo "              </tr>";
                        
        }   

        if ($i==0) {
            echo "<tr class='even'><td colspan='3'><h3>No Results.</h3></td></tr>";
        }

But my code is entering the if statement.
How can I go through the result?
The database is MySQL.

<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname) or die('Did you <a href="setupreset.php">setup/reset the DB</a>? <p><b>SQL Error:</b>' . mysql_error($conn) . '<p><b>SQL Statement:</b>' . $query);

function execute_query($the_query){
    global $conn;
    $result = mysql_query($the_query) or die('<font size=2><b>SQL Error:</b>' . mysql_error($conn) . '<br/><p><b>SQL Statement:</b>' . $the_query . '<br/>Did you run <i>setupreset.php</i> to setup/reset the DB?</font>');
    return $result;
}
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

婴鹅 2025-02-07 10:03:42

您需要将结果绑定到一些变量:

$query = "SELECT pcode,price,description FROM products WHERE description like ? ORDER BY PRICE";
$stmt = $mysqli->prepare($query);

$search_criteria = '%'.$search_criteria.'%';                
$stmt->bind_param('s', $search_criteria);
$stmt->execute();   

$i=0; $pcode; $price; $description;
$stmt->bind_result($pcode, $price, $description);

while($stmt->fetch()) {
    if ($i++%2 == 0) 
        $trow = "even"; 
    else 
        $trow = "odd"; 
    echo "              <tr class='{$trow}'>";
    echo "                  <td>{$pcode}</td><td>{$price}</td><td>{$description}</td>";
    echo "              </tr>";
}   

You need to bind the result to some variables:

$query = "SELECT pcode,price,description FROM products WHERE description like ? ORDER BY PRICE";
$stmt = $mysqli->prepare($query);

$search_criteria = '%'.$search_criteria.'%';                
$stmt->bind_param('s', $search_criteria);
$stmt->execute();   

$i=0; $pcode; $price; $description;
$stmt->bind_result($pcode, $price, $description);

while($stmt->fetch()) {
    if ($i++%2 == 0) 
        $trow = "even"; 
    else 
        $trow = "odd"; 
    echo "              <tr class='{$trow}'>";
    echo "                  <td>{$pcode}</td><td>{$price}</td><td>{$description}</td>";
    echo "              </tr>";
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文