时间:2021-07-01 10:21:17 帮助过:13人阅读
使用php连接数据库
1. 向数据表student,插入一条信息:"小明", 28, "男" 添加成功输出提示"添加成功" 2. 插入多条信息1 <?php 2 header("Content-type:text/html;charset=utf-8"); 3 //链接数据库 4 $conn =@mysqli_connect("localhost","root","","lanou0322"); 5 if(!$conn){ 6 echo "失败"; 7 // 终止 8 exit; 9 } 10 $conn->query(‘set names utf8‘); 11 //向数据库添加数据INSERT INTO name(数据表名) VALUE (添加的数据信息); 12 $sql ="INSERT INTO student(name,sex,age) VALUES (‘小黄‘,‘女‘,‘17‘)"; 13 $sql ="SELECT * FROM student"; 14 $result = $conn->query($sql); 15 // 判断插入是否成功 16 // 使用 mysqli_affected_rows($conn); 17 if(mysqli_affected_rows($conn)>0){ 18 echo "成功"; 19 }else{ 20 echo "失败"; 21 } 22 echo mysqli_num_rows($result); 23 if(mysqli_num_rows($result)>0){ 24 echo "<table border=1>"; 25 echo "<tr> 26 <th>id</th> 27 <th>name</th> 28 <th>sex</th> 29 <th>age</th> 30 </tr>"; 31 while($row = $result -> fetch_assoc()){ 32 echo "<tr> 33 <td style=‘width:20‘>{$row[‘id‘]}</td> 34 <td style=‘width:80; text-align:center;‘>{$row[‘name‘]}</td> 35 <td style=‘width:20‘>{$row[‘sex‘]}</td> 36 <td style=‘width:20‘>{$row[‘age‘]}</td> 37 </tr>"; 38 } 39 echo "</table>"; 40 } 41 $conn->close();//关闭数据库 42 ?>View Code 效果: 案例3:更新update 使用php连接数据库 更新(修改)数据表student中,年龄age=28的改为10
1 <?php 2 header("Content-type:text/html;charset=utf-8"); 3 $conn =@mysqli_connect("localhost","root","","lanou0322"); 4 $conn->query(‘set names utf8‘); 5 // mysqli_query($conn,"UPDATA name SET... WHERE ...") 6 $sql = mysqli_query($conn,"UPDATE student SET name = ‘我傻逼‘,age = ‘10‘ WHERE id = 1"); 7 $sql ="SELECT * FROM student"; 8 $result = $conn->query($sql); 9 echo mysqli_affected_rows($conn); 10 // 判断修改是否成功 11 if(mysqli_affected_rows($conn)>0){ 12 echo "成功"; 13 }else{ 14 echo "失败"; 15 } 16 echo mysqli_num_rows($result); 17 if(mysqli_num_rows($result)>0){ 18 echo "<table border=1>"; 19 echo "<tr> 20 <th>id</th> 21 <th>name</th> 22 <th>sex</th> 23 <th>age</th> 24 </tr>"; 25 while($row = $result -> fetch_assoc()){ 26 echo "<tr> 27 <td style=‘width:20‘>{$row[‘id‘]}</td> 28 <td style=‘width:80; text-align:center;‘>{$row[‘name‘]}</td> 29 <td style=‘width:20‘>{$row[‘sex‘]}</td> 30 <td style=‘width:20‘>{$row[‘age‘]}</td> 31 </tr>"; 32 } 33 echo "</table>"; 34 } 35 $conn->close(); 36 ?>View Code 效果: 案例4:删除delete 使用php连接数据库class9, 删除数据库的表student中,id号为1的数据
1 <?php 2 header("Content-type:text/html;charset=utf-8"); 3 $conn =@mysqli_connect("localhost","root","","lanou0322"); 4 $conn->query(‘set names utf8‘); 5 $sql = mysqli_query($conn,"DELETE FROM student WHERE id = 1"); 6 $sql ="SELECT * FROM student"; 7 $result = $conn->query($sql); 8 // echo mysqli_affected_rows($conn); 9 // 判断修改是否成功 10 if(mysqli_affected_rows($conn)>0){ 11 echo "成功"; 12 }else{ 13 echo "失败"; 14 } 15 // echo mysqli_num_rows($result); 16 if(mysqli_num_rows($result)>0){ 17 echo "<table border=1>"; 18 echo "<tr> 19 <th>id</th> 20 <th>name</th> 21 <th>sex</th> 22 <th>age</th> 23 </tr>"; 24 while($row = $result -> fetch_assoc()){ 25 echo "<tr> 26 <td style=‘width:20‘>{$row[‘id‘]}</td> 27 <td style=‘width:80; text-align:center;‘>{$row[‘name‘]}</td> 28 <td style=‘width:20‘>{$row[‘sex‘]}</td> 29 <td style=‘width:20‘>{$row[‘age‘]}</td> 30 </tr>"; 31 } 32 echo "</table>"; 33 } 34 $conn->close(); 35 ?>View Code
效果:
关于PHP数据库mysql的一些案例
标签:family host important 技术分享 nec 链接 imp upd 函数