mysqli_stmt :: bind_param():变量与参数数量不匹配

时间:2019-07-25 15:42:02

标签: php mysql

当我尝试执行代码时,出现错误: mysqli_stmt :: bind_param():变量数与准备好的语句中的参数数不匹配

我的目标:通过过滤所需的列从数据库表中提取信息并将其添加到数组中。

我已经搜索了相同错误的答案,但是提供的解决方案(例如使用'?'已经实现)

我的DbOperation.php

function getPersonChanges_made($login_token){
        $stmt = $this->con->prepare("SELECT id, first_name, last_name, login_token, time_started, time_ended, todays_worktime FROM changes_made WHERE login_token = ? ");
        $stmt->bind_param("s", $login_token);
        $stmt->execute();
        $arrs = array(); 

        while($stmt->fetch()){
            $arr  = array();
            $arr['id'] = $id; 
            $arr['first_name'] = $first_name; 
            $arr['last_name'] = $last_name; 
            $arr['login_token'] = $login_token; 
            $arr['time_started'] = $time_started;
            $arr['time_ended'] = $time_ended;
            $arr['todays_worktime'] = $todays_worktime;

            array_push($arrs, $arr); 
        }

        return $arrs;  
    }

这是我的Api.php

case 'getpersonchanges_made':
                 isTheseParametersAvailable(array('login_token'));
                $db = new DbOperation();
                $result = $db->getPersonChanges_made(
                     $_POST['login_token']
                );

                if($result){
                    $response['error'] = false; 
                    $response['message'] = 'success';
                    $response['arrs'] = $db->getPersonChanges_made($login_token);
                }else{
                    $response['error'] = true; 
                    $response['message'] = 'error';
                }
                break;

3 个答案:

答案 0 :(得分:0)

您只需要绑定一个参数,因为查询中只有一个占位符:

awk -F'[,;]' -v test="$test" '
    FNR==1 { doneHdr = 0 }
    $0 ~ test {
        if ( !doneHdr++ ) {
            split(FILENAME,a,/[-.]/)
            print "DATE ========================== : " a[4]
        }
        print ""
        print "LPARS :" $2
        print "RAM : " $5
        print "CPU 1 : " $6
        print "CPU 2 : " $7
        print "" 
        print ""
    }
' /var/www/cgi-bin/LPAR_MAP/*

答案 1 :(得分:-1)

在执行之前必须绑定。您在执行后绑定了,这是行不通的。

$stmt->bind_param("s", $login_token);
$stmt->execute();

用于逐行获取数据并构建$arrs

while($row = $stmt->fetch()) {
    $arr  = array();
    $arr['id'] = $row['id'];
    $arr['first_name'] = $row['first_name'];
    $arr['last_name'] = $row['last_name'];
    $arr['login_token'] = $row['login_token'];
    $arr['time_started'] = $row['time_started'];
    $arr['time_ended'] = $row['time_ended'];
    $arr['todays_worktime'] = $row['todays_worktime'];

    array_push($arrs, $arr);
}

有关更多详细信息,请参见manual

答案 2 :(得分:-1)

在执行准备好的语句后,您可能想使用bind_result-将各个列分配为变量,以便以后提取。如前所述,使用单个占位符意味着您只需要将一个变量绑定为输入参数。

function getPersonChanges_made( $login_token=false ){

    $sql='select id, first_name, last_name, login_token, time_started, time_ended, todays_worktime from changes_made where login_token = ?';
    $stmt = $this->con->prepare( $sql );

    if( $stmt && $login_token ){
        $stmt->bind_param('s',$login_token);
        $res=$stmt->execute();

        if( $res ){

            $stmt->store_result();
            $stmt->bind_result( $id, $first_name, $last_name, $login_token, $time_started, $time_ended, $todays_worktime );
            $arrs=[];

            while( $stmt->fetch() ){
                $arr=[];

                $arr['id'] = $id; 
                $arr['first_name'] = $first_name; 
                $arr['last_name'] = $last_name; 
                $arr['login_token'] = $login_token; 
                $arr['time_started'] = $time_started;
                $arr['time_ended'] = $time_ended;
                $arr['todays_worktime'] = $todays_worktime;

                array_push($arrs, $arr);                
            }
            $stmt->free_result();
            $stmt->close();

            return $arrs;
        }
    }
    return false;
}