Home > Article > Backend Development > How to connect MySQL with PHP to perform add, delete, modify and query operations
This article mainly introduces the method of PHP connecting to MySQL to perform add, delete, modify and query operations. It has a very good reference value. Let’s take a look at it with the editor.
Not much to say , Please see the code:
#<table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> </tr> <?php 1.造一个mysqli对象,造连接对象 $db = new MySQLi("localhost","用户名","密码","数据库名"); 2.准备一条SQL语句 $sql = "select * from info"; 3.执行SQL语句,如果是查询语句,成功返回结果集对象 $reslut = $db->query($sql); 4.判断返回是否执行成功 if($reslut) { while($attr = $reslut->fetch_row()) { echo "<tr> <td>{$attr[0]}</td> <td>{$attr[1]}</td> <td>{$attr[2]}</td> <td>{$attr[3]}</td> <td>{$attr[4]}</td> </tr>"; } } ?> </table>
# FETCH_ALL () Return to all the array
counter ) out out out out out Through
<?php //造连接对象 $db = new MySQLi("localhost","用户名","密码","数据库名"); //准备SQL语句 $sql = "delete from info where code='p004'"; 删 //$sql = "insert course values" 增 //$sql = "update 表名 set 字段=信息 where 字段=信息" 改 //执行SQL语句 $r = $db->query($sql); if($r) { echo "执行成功"; } else { echo "执行失败"; } ?>
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:PHP MYSQL implementation of read-write separation practical detailed explanation
PHP method to connect to
mysqlphp mysql_list_dbs() function usage detailed explanation
The above is the detailed content of How to connect MySQL with PHP to perform add, delete, modify and query operations. For more information, please follow other related articles on the PHP Chinese website!