致命错误:未捕获的 ArgumentCountError:类型定义字符串中的元素数量必须与绑定变量的数量匹配

问题描述 投票:0回答:1

我在执行准备好的语句时遇到此错误。 但我对所有变量进行了很多次计数,但无法找到错误。 本例中的查询字符串是: 收据.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

所以:

  • 4 where 子句
  • 4 种参数类型
  • 4 个参数值

我到底做错了什么?

php mysql prepared-statement
1个回答
0
投票

您需要将绑定参数作为单独的值传递。您可以构建一个数组并使用分散数组来传递值:

$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);
© www.soinside.com 2019 - 2024. All rights reserved.