0

我有以下代码,我收到错误:

警告:mysqli_stmt::bind_result() [mysqli-stmt.bind-result]:绑定变量的数量与文件中准备好的语句中的字段数量不匹配。

如果这只是一个警告,代码不应该仍然有效吗?我想做一个选择 * 并显示除一个字段之外的所有数据,我想分别绑定和处理。有什么办法,或者更好的方法吗?我目前的解决方案(未尝试)是使用 getRecords 将正确数量的变量绑定到结果,然后使用 getHtml 单独绑定为不同的名称。

绑定有什么好处,有没有必要。

<?php
if (isset($_GET["cmd"]))
  $cmd = $_GET["cmd"]; else
  die("You should have a 'cmd' parameter in your URL");
$id = $_GET["id"];
$con = mysqli_connect("localhost", "user", "password", "db");
if (!$con) {
    echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
    exit;
}
$con->set_charset("utf8");
echo "test outside loop";
if($cmd=="GetSaleData") {
    echo "test inside loop";
    if ($getRecords = $con->prepare("SELECT SELECT Product_NO, Product_NAME, SUBTITLE, CURRENT_BID, START_PRICE, BID_COUNT, QUANT_TOTAL, QUANT_SOLD, ACCESSSTARTS, ACCESSENDS, ACCESSORIGIN_END, USERNAME, BEST_BIDDER_ID, FINISHED, WATCH, BUYITNOW_PRICE, PIC_URL, PRIVATE_AUCTION, AUCTION_TYPE, ACCESSINSERT_DATE, ACCESSUPDATE_DATE, CAT_DESC, CAT_PATH, COUNTRYCODE, LOCATION, CONDITIONS, REVISED, PAYPAL_ACCEPT, PRE_TERMINATED, SHIPPING_TO, FEE_INSERTION, FEE_FINAL, FEE_LISTING, PIC_XXL, PIC_DIASHOW, PIC_COUNT, ITEM_SITE_ID FROM Sales WHERE Product_NO = ?")) FROM SaleS WHERE PRODUCT_NO = ?")) {
        $getHtml = $con->prepare("SELECT PRODUCT_DESC FROM SaleS WHERE PRODUCT_NO = ?");
        $getHtml->bind_param("i", $id);
        $getHtml->execute();
        $getHtml->bind_result($PRODUCT_DESC);
        $getRecords->bind_param("i", $id);
        $getRecords->execute();
        $getRecords->bind_result($PRODUCT_NO, $PRODUCT_NAME, $SUBTITLE, $CURRENT_BID, $START_PRICE, $BID_COUNT, $QUANT_TOTAL, $QUANT_SOLD, $ACCESSSTARTS, $ACCESSENDS, $ACCESSORIGIN_END, $USERNAME, $BEST_BIDDER_ID, $FINISHED, $WATCH, $BUYITNOW_PRICE, $PIC_URL, $PRIVATE_Sale, $Sale_TYPE, $ACCESSINSERT_DATE, $ACCESSUPDATE_DATE, $CAT_DESC, $CAT_PATH, $COUNTRYCODE, $LOCATION, $CONDITIONS, $REVISED, $PAYPAL_ACCEPT, $PRE_TERMINATED, $SHIPPING_TO, $FEE_INSERTION, 
        $FEE_FINAL, $FEE_LISTING, $PIC_XXL, $PIC_DIASHOW, $PIC_COUNT, $ITEM_SITE_ID);
        while ($getRecords->fetch()) {
            // operations snipped for question
            echo "<h1>".$PRODUCT_NAME."</h1>
<div id='leftlayer' class='leftlayer'>
<p><strong>Username: </strong>".$USERNAME."
<p><strong>Shipping to: </strong> ". $country ."
<img src='./images/".$id.".jpg"' width='".$imageSize["width"]."' height='".$imageSize["height"]."'>
</div>
<div id='rightlayer'> 
</div>";
//
} 
}
}
}

我也想知道我的 img src 语句有什么问题......,我觉得我错过了一些基本的东西,比如斜线或引号。

编辑:代码现在在将 * 替换为列名后显示错误,但是从未输入过 while 循环,因此不执行任何操作。

4

3 回答 3

0

根据 MySQLi bind_result() 的示例,您可能需要在 SELECT 中明确声明列名,而不是使用 * 。

关于 img 行,您在 .jpg 之后有一个额外的 "

<img src='./images/".$id.".jpg"' width=

应该

<img src='./images/".$id.".jpg width=

此外,您实际上不需要拆分字符串并连接变量,因为使用 " 将导致字符串被解析为变量。

于 2009-02-02T13:35:51.260 回答
0

我猜问题出在这里:

$getRecords->bind_result($PRODUCT_NO, $PRODUCT_NAME, $SUBTITLE, $CURRENT_BID, $START_PRICE, $BID_COUNT, $QUANT_TOTAL, $QUANT_SOLD, $ACCESSSTARTS, $ACCESSENDS, $ACCESSORIGIN_END, $USERNAME, $BEST_BIDDER_ID, $FINISHED, $WATCH, $BUYITNOW_PRICE, $PIC_URL, $PRIVATE_Sale, $Sale_TYPE, $ACCESSINSERT_DATE, $ACCESSUPDATE_DATE, $CAT_DESC, $CAT_PATH, $COUNTRYCODE, $LOCATION, $CONDITIONS, $REVISED, $PAYPAL_ACCEPT, $PRE_TERMINATED, $SHIPPING_TO, $FEE_INSERTION, 
        $FEE_FINAL, $FEE_LISTING, $PIC_XXL, $PIC_DIASHOW, $PIC_COUNT, $ITEM_SITE_ID);

您可能缺少一列。如果您的列数少于结果集中检索到的列数,则该代码将起作用。

至于 img 标签,如果您在 xhmtl 上,则必须将其关闭。

于 2009-02-02T13:37:25.140 回答
0

我将从修复这条线开始:

if ($getRecords = $con->prepare("SELECT SELECT Product_NO, Product_NAME, SUBTITLE, CURRENT_BID, START_PRICE, BID_COUNT, QUANT_TOTAL, QUANT_SOLD, ACCESSSTARTS, ACCESSENDS, ACCESSORIGIN_END, USERNAME, BEST_BIDDER_ID, FINISHED, WATCH, BUYITNOW_PRICE, PIC_URL, PRIVATE_AUCTION, AUCTION_TYPE, ACCESSINSERT_DATE, ACCESSUPDATE_DATE, CAT_DESC, CAT_PATH, COUNTRYCODE, LOCATION, CONDITIONS, REVISED, PAYPAL_ACCEPT, PRE_TERMINATED, SHIPPING_TO, FEE_INSERTION, FEE_FINAL, FEE_LISTING, PIC_XXL, PIC_DIASHOW, PIC_COUNT, ITEM_SITE_ID FROM Sales WHERE Product_NO = ?")) FROM SaleS WHERE PRODUCT_NO = ?")) {

我假设它应该看起来像这样:

if ($getRecords = $con->prepare("SELECT Product_NO, Product_NAME, SUBTITLE, CURRENT_BID, START_PRICE, BID_COUNT, QUANT_TOTAL, QUANT_SOLD, ACCESSSTARTS, ACCESSENDS, ACCESSORIGIN_END, USERNAME, BEST_BIDDER_ID, FINISHED, WATCH, BUYITNOW_PRICE, PIC_URL, PRIVATE_AUCTION, AUCTION_TYPE, ACCESSINSERT_DATE, ACCESSUPDATE_DATE, CAT_DESC, CAT_PATH, COUNTRYCODE, LOCATION, CONDITIONS, REVISED, PAYPAL_ACCEPT, PRE_TERMINATED, SHIPPING_TO, FEE_INSERTION, FEE_FINAL, FEE_LISTING, PIC_XXL, PIC_DIASHOW, PIC_COUNT, ITEM_SITE_ID FROM Sales WHERE Product_NO = ?")) {
于 2009-02-02T15:47:15.380 回答