1 <?php 2 header("content-type:text/html;charset=utf-8"); 3 $conn = new mysqli("localhost","root","root","test"); //ip、username、password、dbname 4 $conn->query("set names utf8"); //设置编码 5 6 $sql = "SELECT * FROM TEST"; 7 $result = $conn->query($sql); 8 9 while ($row = $result->fetch_assoc()) { 10 echo $row["name"]; 11 echo "<br/>"; 12 } 13 while ($row = $result->fetch_row()) { 14 echo $row[0]; 15 echo "<br/>"; 16 } 17 18 $sql = "INSERT INTO TEST(id) VALUES(2)"; 19 $conn->query($sql); 20 if($conn->affected_rows>0) { 21 echo $conn->insert_id; 22 } else { 23 echo -1; 24 } 25 26 $conn->autocommit(FALSE); //默认是自动提交的,设置为不自动提交,才可以使用事务 27 $sql1 = "....."; 28 $sql2 = "....."; 29 $conn->query($sql1); 30 $conn->query($sql2); 31 if(!$conn->errno) { 32 $conn->commit(); 33 } else { 34 $conn->rollback(); 35 } 36 37 ?>