SoFunction
Updated on 2025-03-09

PHP connects to MySQL for addition, deletion, modification and inspection operations

Without further ado, please see the code:

<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>Codename</td>
<td>Name</td>
<td>gender</td>
<td>nationality</td>
<td>Birthday</td>
</tr>
<?php
1.Make onemysqliObject,造连接Object
$db = new MySQLi("localhost","username","password","Database Name");
2.Prepare oneSQLStatement
$sql = "select * from info";
3.implementSQLStatement,如果是查询Statement,成功返回结果集Object
$reslut = $db->query($sql);
4.判断返回是否implement成功
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()

fetch_row()

fetch_assoc()

fetch_object()                                                            �

fetch_array()                  The returned array is both indexed and associated

Delete, add, modify the database

<?php
//Create connection objects$db = new MySQLi("localhost","username","password","Database Name");
//Prepare SQL statement$sql = "delete from info where code='p004'";    delete
//$sql = "insert course values"//$sql = "update table name set field = information where field = information" Change//Execute SQL statement$r = $db->query($sql);
if($r)
{
echo "Execution successful";
}
else
{
echo "Execution failed";
}
?>

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!