mysqli绑定参数的错误

时间:2012-07-28 05:15:25

标签: php mysql

我的数据库中有两个表。当我执行下面的PHP脚本时,我遇到了错误。我认为查询是正确的,但我如何绑定参数错误?

Display
+-------+------------+-------------+
| Index | DISPLAY_ID | Picture_ID  |
+-------+------------+-------------+
|     1 |         12 | longblob    |
+-------+------------+-------------+

Cards
+--------+------------+------------+-----------------+
| Card_ID| DISPLAY_ID | Card_Type  |  Card_name      |
+--------+------------+------------+-----------------+
|        |            |            |                 |
+--------+------------+---------=--+-----------------+

 <?php
$mysqli=mysqli_connect('localhost','root','','draftdb');
if (!$mysqli)
die("Can't connect to MySQL: ".mysqli_connect_error());


$stmt = $mysqli->prepare("SELECT cards.CARD_TYPE, display.PICTURE_ID 
FROM cards  
INNER JOIN display ON cards.DISPLAY_ID = display.DISPLAY_ID 
WHERE display.DISPLAY_ID=? AND cards.CARD_TYPE =?" );


$displayid=11;
$cardtype='Mythic';

$stmt->bind_param("si", $displayid, $cardtype);
$stmt->execute();
$stmt->bind_result($image);
$stmt->fetch();
//header("Content-Type: image/jpeg");
echo $image; 
?>
  

警告:mysqli_stmt :: bind_result()::绑定变量数与预备语句中的字段数不匹配


修复:如果您正在从数据库中读取图片,请确保仅在查询中返回blob数据。 (从SELECT语句中删除cards.CARD_TYPE。)

2 个答案:

答案 0 :(得分:4)

尝试以下两种方法:

  • 摆脱:if (!$mysqli->query($stmt)) { printf("Errormessage: %s\n", $mysqli->error); }
    您在致电execute()
  • 时执行查询
  • 您需要绑定2个参数,然后传递两个但只列出一个类型。将bind_param来电更改为:
    $stmt->bind_param("si", $cardtype, $displayid); // s for string, i for int

编辑:尝试将代码更改为:

$stmt = $mysqli->prepare("SELECT cards.CARD_TYPE, display.PICTURE_ID 
FROM cards  
INNER JOIN display ON cards.DISPLAY_ID = display.DISPLAY_ID 
WHERE display.DISPLAY_ID=? AND cards.CARD_TYPE =?" );


$displayid=11;
$cardtype='Mythic';

$stmt->bind_param("is", $displayid, $cardtype);
$stmt->execute();

答案 1 :(得分:3)

您不应该先进行查询。准备好后,绑定值然后执行它。这就是你必须要做的。

在你的bindparam中,你必须使用“si”,因为你传递了两个值。