【问题标题】:Mysqli prepare query not working [duplicate]Mysqli准备查询不起作用[重复]
【发布时间】:2017-03-03 15:49:45
【问题描述】:

我正在从数据库中获取图像。我编写了下面给出的 php 脚本,只有 while 循环中的“echo”语句有效,但变量未显示来自数据库的值。换句话说,只有 html 有效。

没有任何错误。 不知道是什么问题。

帮帮我。 谢谢 !

$db_username = 'xxxxx';
$db_password = 'xxxx';
$db_name = 'xxxx';
$db_host = 'localhost';
$item_per_page = 9;

$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name);
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

if(!is_numeric($page_number)){
    header('HTTP/1.1 500 Invalid page number!');
    exit();
}

$position = (($page_number-1) * $item_per_page);

$results = $mysqli->prepare("select * FROM inserting_discount_product ORDER BY id DESC LIMIT ?, ?");

$results->bind_param("dd", $position, $item_per_page); 

$results->execute(); //Execute prepared Query

$results->bind_result($pro_id, $pro_title, 
$pro_cat,$pro_desc,$pro_price,$pro_img,$pro_discount_price,$pro_discount_percent
age,$product_code); 


while($results->fetch()){ 
                echo "
                <div id='$pro_id' class='col-md-3 col-sm-4 col-xs-12'>

                <h3 style='color:black'; align='center'>$pro_title</h3> 
                <div>

                <img src='../admin_area/product_images/product_pics/$pro_img'  width='180' height='180' /> <br>
                </div>


                <p style='color:black;margin-right:12px'><b>Price: $  $pro_discount_price </b>

                <span style='color:black;text-decoration:line-through;margin-left:15px'>
                <b style='color:red'>Price: $ $pro_price</b>
                </span>

                </p>

                <a href='details.php?pro_id=$pro_id' style='margin-right:10px' class='detail_hover'><b>Details</b></a>
                <span style='font-weight:bolder'>$pro_discount_percentage %</span>
                <a href='index.php?add_cart=$pro_id&product_code=$product_code' style='color:orange;margin-left:10px' class='basket-logo'>
                <span  class='fa fa-shopping-cart fa-2x'></span></a> 


                    </div>

                ";

}

【问题讨论】:

    标签: php mysql mysqli pdo


    【解决方案1】:

    最好将您的准备语句放在 IF 中,因为如果准备失败,它可以返回一个布尔值。

        if($results = $mysqli->prepare("select * FROM inserting_discount_product ORDER BY id DESC LIMIT ?, ?"))
            {
                $results->bind_param("dd", $position, $item_per_page); 
    
                $results->execute(); //Execute prepared Query
    
                $results->bind_result($pro_id, $pro_title, 
                $pro_cat,$pro_desc,$pro_price,$pro_img,$pro_discount_price,$pro_discount_percentage,$product_code); 
    
                while($results->fetch())
                {
                   // ... stuff
                }
                $results->close();
            }
            else { echo $mysqli->error;  }
    

    您可能还应该在 SELECT 中显式声明您的返回列,而不是使用 *。下一行你可能会在表中添加一列,如果你正在执行 SELECT *,你的所有代码都会立即中断,因为查询将返回比 bind_result 中的变量更多的列。

    【讨论】:

    • 在用您建议的语法替换语法并添加列名而不是 '*' 后出现此错误“允许的内存大小为 67108864 字节已用尽(尝试分配 4294967296 字节)”
    • 好的,我以前见过这个错误。原因是表的定义,您尝试选择的列之一是非常大的类型。我在使用 LONGTEXT 类型而不是 TEXT 时得到它。
    【解决方案2】:

    看这里:

    What is the difference between single-quoted and double-quoted strings in PHP?

    这是您在 echo 语句中的引用的问题。你有双引号和单引号的混合。在 php 中,单引号将按原样显示内容,因此您的变量不会被显示。因此,您的 echo 语句需要分解为串联,以便正确显示您想要的内容。请注意,php 使用点运算符进行连接。

    例如:

    echo "<div id='".$pro_id."' class='col-md-3 col-sm-4 col-xs-12'>
    

    没有任何错误的原因是因为它假定您要回显的所有变量都应该以纯文本的形式回显,因此它实际上是将“$pro_id”作为元素的 id 而不是变量的内容写入 html是。

    或者,您的标记是很多 html。您可以将整个 while 循环转移到 html 并使用 php 短代码在循环中获取变量,如下所示:

    while($results->fetch()){ 
        //End php here, go into HTML markup.
        ?>
    
         <div id='<?=$pro_id?>' class='col-md-3 col-sm-4 col-xs-12'>
    
        ...
    
        <?php
    //Don't forget to add the closing brace to the while loop back in <?php
    }
    

    此方法使用此处显示的 php 简写符号:这在 html 标记中将 php 变量回显到页面。这减少了您必须处理的引号和字符串的数量,并且更容易使用,因为这是直接 HTML。如果您的大部分内容都将是 HTML(它是)并且您只需要添加一些 PHP 变量,我肯定会建议关闭您的 php?> 并使用速记。

    【讨论】:

    • 仅作记录:没有编辑可以挽救这个答案,这与问题无关并且本身就是错误的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2015-01-05
    相关资源
    最近更新 更多