Mysqli php

Query Select

$query = $mysqli->query("SELECT * FROM table");

// leggo i dati
while($row = $query->fetch_array(MYSQL_ASSOC)){
    echo'<p>'.$row['id'].'</p>';
}

// conto le righe 
$nr = $query->num_rows;

Transaction

$mysql->begin_transaction();

$query1 = $mysqli->prepare("INSERT INTO table1(tipo) VALUES('cliente')");
$res1 0 $query1->execute();
if(! $res1){
    die('Errore nella query1:'. $mysqli->error);
}
else{
    $id = $mysqli->insert_id;
}

$query2 = $mysqli->prepare("INSERT INTO table2(name, surname) VALUES('Glauco','Guarnieri')");
$res2 = $query->execute();
if(! res2) echo'Errore nella query2: '. $mysqli->error;

if(! $res1 || ! $res2){
    $mysqli->rollback();
}
else{
    $mysqli->commit();
}

Lascia un commento

Il tuo indirizzo email non sarĂ  pubblicato. I campi obbligatori sono contrassegnati *