Home  >  Article  >  Backend Development  >  PHP and MySQL - delete operation

PHP and MySQL - delete operation

巴扎黑
巴扎黑Original
2016-11-30 09:30:121067browse

<h1>删除记录</h1>
<?php
//连接数据库的参数
$host = "localhost";
$user = "root";
$pass = "zq19890319";
$db = "phpdev";
//创建mysqli对象
//打开一个数据库连接
$mysqli = new mysqli($host, $user, $pass, $db);
//检查连接错误
if(mysqli_connect_errno()){
die("Unable to connect!");
}
//如果记录ID存在,则执行删除操作
if(isset($_GET[&#39;id&#39;])){
//构造删除记录的SQL
$query = "DELETE FROM symbols WHEREid = ".$_GET[&#39;id&#39;];
//执行SQL查询
if($mysqli->query($query)){
//显示删除后影响的记录数
echo $mysqli->affected_rows. "row(s) affected";
}
else
{
//如果没有匹配的记录,显示数据库返回的错误信息
echo "Error in query : $query. ".$mysqli->error;
}
}
//构造显示删除后的记录
$query = "SELECT * FROM symbols";
//执行该查询
if($result=$mysqli->query($query)){
//显示返回的记录集行数
if($result->num_rows>0){
//如果有记录
//显示记录集中列的内容
echo "<table cellpadding=10 border=1>";
while($row=$result->fetch_array()){
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td><a href=".$_SERVER[&#39;PHP_SELF&#39;]." mce_href=".$_SERVER[&#39;PHP_SELF&#39;]."?id=".$row[0].">删除</a></td>";
echo "</tr>";
}
}
//释放对象所占用的内存
$result->close();
}
else
{
//输出数据库错误的信息
echo "Error in query: $query. ".$mysqli->error;
}
//关闭数据库
$mysqli->close();
?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php skymvc a simple phpNext article:php skymvc a simple php