参数号无效:没有绑定参数

时间:2016-04-12 12:22:40

标签: php pdo

$names = array(array("name"=>"kjbedi12","age"=>20),array("name"=>"jasrajbedi12","age"=>22));
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // /*set the PDO error mode to exception*/
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // echo $name;
    $query = "INSERT INTO names (name, age) VALUES "; //Prequery
    $qPart = array_fill(0, count($names), "(?, ?)");
    $query .=  implode(",",$qPart);
    $stmt = $conn -> prepare($query); 
    $i = 1;
    foreach($names as $item) { //bind the values one by one
       // var_dump($item);
       $stmt->bindValue($i++, $item['name']);
       $stmt->bindValue($i++, $item['age']);
    }
    echo $query;
    $stmt = $conn->prepare($query);
    $conn->beginTransaction();  
    $stmt->execute();
    $conn->commit();
    echo "New records created successfully";

此代码返回错误

  

SQLSTATE [HY093]:参数号无效:没有绑定参数

这个错误?我做错了什么?

0 个答案:

没有答案