im与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: %sn", $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();
所以,我不知道函数开始发射是否错误或其他。
在连接而不是语句上必须是 $mysqli->begin_transaction();
。通过交易,您想执行多个单个语句。
http://php.net/manual/en/mysqli.begin-transaction.php