我在执行准备好的语句时遇到此错误。 但我对所有变量进行了很多次计数,但无法找到错误。 本例中的查询字符串是: 收据.php?type=1|11|12|15
代码下方:
$pag = 1; //page initialising
if ( isset( $_GET[ 'pag' ] ) ) {
$pag = $_GET[ 'pag' ];
}
$resultsForPage = 20;
$startRecord = $pag * $resultsForPage - $resultsForPage;
$type = explode( "|", $_GET[ "type" ] ); //explode all the types in an array
$where = ' WHERE R.TipoRicetta = ? '; //initialize the where clause for the first item
$paramType = 'i';
$paramValue = $type[ 0 ];
//adding the other types
for ( $i = 1; $i < count( $type ); $i++ ) {
$paramType .= 'i';
$paramValue .= ',' . $type[ $i ];
$where .= 'OR R.TipoRicetta = ? ';
}
$query = 'SELECT R.*, F.urlFoto, D.descrizione AS diff FROM ricette R
INNER JOIN ricFoto RF ON RF.idRicetta = R.IDContenuto
INNER JOIN foto F ON F.idFoto = RF.idFoto
INNER JOIN Tb_Difficolta D ON R.Difficolta = D.IDDifficolta' . $where . 'LIMIT ' . $startRecord . ',' . $resultsForPage;
以下是此代码创建的查询:
SELECT R.*, F.urlFoto, D.descrizione AS diff FROM ricette R INNER JOIN ricFoto RF ON RF.idRicetta = R.IDContenuto INNER JOIN foto F ON F.idFoto = RF.idFoto INNER JOIN Tb_Difficolta D ON R.Difficolta = D.IDDifficolta WHERE R.TipoRicetta = ? OR R.TipoRicetta = ? OR R.TipoRicetta = ? OR R.TipoRicetta = ? LIMIT 0,20
打印在屏幕上
$paramType 值:iiii
$参数值:1,11,12,15
所以:
我到底做错了什么?
您需要将绑定参数作为单独的值传递。您可以构建一个数组并使用分散数组来传递值:
$paramValue = [$type[ 0 ]];
//adding the other types
for ( $i = 1; $i < count( $type ); $i++ ) {
$paramType .= 'i';
$paramValue[] = $type[ $i ];
$where .= 'OR R.TipoRicetta = ? ';
}
.....
$stmt->bind_param($paramType, ...$paramValue);