事务出错:非对象

时间:2016-06-24 17:01:53

标签: php mysqli prepared-statement

我正在使用PDO并准备语句,现在我想实现事务,但是当我这样做时,我得到了这个错误:

Call to a member function beginTransaction() on a non-object in

代码是:

include_once '../../includes/db_connect.php';
include_once '../../includes/psl-config.php';
$mysqli->query("SET NAMES 'utf8'");
date_default_timezone_set('America/Mexico_City');
$hora = date("d-m-Y g:i a");

$mysqli->beginTransaction();

//OBTENCIÓN DE LA INFORMACIÓN
$id = $mysqli->real_escape_string( $_POST["id"] );
$fechaEntrada = date("Y-m-d");
$estatus = "Disponible";
$idUser = $mysqli->real_escape_string( $_POST['idUser'] );
$idUser = $idUser . "  " . $hora;

//SELECIONAR INFORMACIÓN DE LA MAQUINA A ENTRAR
$sqlMaquina = "SELECT tipo, clave FROM maquinas_orden_renta WHERE id = $id";
$res = $mysqli->query($sqlMaquina);
$rows = $res->num_rows;
if (!$res)
    printf("Error message: %s\n", $mysqli->error);
$objMaquinaRenta = $res->fetch_object();
$tipoMaquina = $objMaquinaRenta->tipo;
$idMaquina = $objMaquinaRenta->clave;

$sql = "UPDATE maquinas_orden_renta SET entradaReal = ?, estatus = ?,updated_by = ? WHERE id = ?";
$stmt = $mysqli->prepare($sql);
if ( false === $stmt )
    die('prepare() failed: ' . htmlspecialchars($mysqli->error));

$rc = $stmt->bind_param('sssi', $fechaEntrada, $estatus, $idUser, $id );
if ( false === $rc )
    die('bind_param() failed: ' . htmlspecialchars($stmt->error));

$rc = $stmt->execute();


if ( false === $rc )
    die('execute() failed: ' . htmlspecialchars($stmt->error));
else{
    if ($tipoMaquina == "conID") {
        $sql2 = "UPDATE maquinas SET estatus = ?, updated_by = ? WHERE IdMaquina = ?";
        $stmt = $mysqli->prepare($sql2);
        if ( false === $stmt )
            die('prepare2() failed: ' . htmlspecialchars($mysqli->error));

        $rc2 = $stmt->bind_param('ss', $estatus, $idUser, $idMaquina );
        if ( false === $rc2 )
            die('bind_param2() failed: ' . htmlspecialchars($stmt->error));

        $rc2 = $stmt->execute();

        if ( false === $rc2 )
            die('execute2() failed: ' . htmlspecialchars($stmt->error));
        else
            echo "Éxito";   
    }
}
if (false === $rc AND  false === $rc2) {
    echo "exito";
    $mysqli->commit();
}else{
    $mysqli->rollBack();
    echo "error: $error";
}

$stmt->close();

所以,我不知道函数beginTransaction是错还是什么的。

1 个答案:

答案 0 :(得分:0)

连接上必须是$mysqli->begin_transaction();而不是语句。对于一个事务,您希望执行多个单一语句。

http://php.net/manual/en/mysqli.begin-transaction.php