views:

1166

answers:

3

Hello, I have the following code, for which I get the error:

Warning: mysqli_stmt::bind_result() [mysqli-stmt.bind-result]: Number of bind variables doesn't match number of fields in prepared statement in file.

If this is only a warning, shouldn't the code still work? I want to do a select * and display all the data except one field, which I want to bind and handle separately. Is there any way around, or a better method? My solution at the moment(untried) is to bind the correct amount of variables to the results with getRecords, and then bind separately as a different name with getHtml.

What are the advanatges of binding, and is it necessary.

<?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>";
//
} 
}
}
}

I would also like to know what is wrong with my img src statement..., I feel I am mising something elementary such as a slash or quote.

edit: The code now displays an error after replace * with column names, however the while loop is never entered, and hence no actions are performed.

+1  A: 

You may need to explicity state the column names in your SELECT rather than use *, as per the examples for MySQLi bind_result().

In relation to the img line, you have an extra " after .jpg

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

should be

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

Also, you don't actually need to break out of the string and concatenate the variables because using " will cause the string to be parsed for variables anyway.

DavidM
+1  A: 

I'm guessing the problem is here:

$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);

You are probably missing a column. The code will work if you have less columns than the retrieved in the result set.

As for the img tag, if you are on xhmtl you will have to close it.

Miguel Ping
I am missing a column deliberatley, PRODUCT_DESC. Do you mean to say in your answer the code will not work if I have less columns than the retrieved in the result set?
Joshxtothe4
I meant the opposite, if you put more columns the code will not work. Less columns should work.
Miguel Ping
How could I be missing a column? I have less than what would be returned by select *.., I don't understand.
Joshxtothe4
Using less columns made no difference
Joshxtothe4
A: 

I would start by fixing this line:

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 = ?")) {

I'm assuming it should look like this instead:

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 = ?")) {
R. Bemrose