在php-mysql中保存并迭代数组

时间:2018-02-21 15:03:14

标签: php html

我有以下代码在我的数据库中保存数组:

public function agregarBolsasModel($datos, $tabla) {
        $datosL=array($datos["valoresBox"]);
        $stmt = Conexion::conectar() -> prepare("INSERT INTO $tabla(lote, caja, bolsa)VALUES(:one, :two, :three)");

        for($p = 0; $p < sizeof($datosL); $p++) {
            $stmt -> bindParam(":one", $datos["loteNum"], PDO::PARAM_INT);
            $stmt -> bindParam(":two", $datos["cajaNum"], PDO::PARAM_INT);
            $stmt -> bindParam(":three", $datosL[$p], PDO::PARAM_INT);
            $stmt->execute();
        }

        $stmt->close();
}

three的值是[1,2,3]这样的数组,我不知道它的大小是因为它是一个动态数组,但我无法保存数据我的数据库,当我试图保存数据时,我收到了以下错误:

  

致命错误&gt;:未捕获的错误:调用未定义的方法PDOStatement :: close()

我的错误是什么?我该怎么做才能解决这个问题?

感谢名单

2 个答案:

答案 0 :(得分:5)

PDO没有“关闭”功能。你可以做到

$ this-&gt; db = null;

在你的情况下,在$ stmt

答案 1 :(得分:2)

使用$ stmt = null; 而不是$ stmt-&gt; close();