Home > php教程 > php手册 > php中删除数据库数据例子

php中删除数据库数据例子

WBOY
Release: 2016-05-25 16:38:14
Original
1453 people have browsed it

php除了删除文件目录不用用到mysql中的delete之外只要删除mysql数据库中的数据都要用到数据库的命令了,下面我看利用delete来删除数据的例子.

sql有许多对数据库操作的语句,但是常见和比较需要的是这么几个语句 DELETE FROM 语句用于从数据库表中删除记录.

语句:DELETE FROM table_name WHERE column_name = some_value

解说:查询表为:table_name 的字段 column_name 的值为:some_value 的值 

例如:删除表 MyClass中编号为1 的记录

mysql> delete from MyClass where id=1;

PHP实例,单条删除,代码如下:

<?php 
$con = mysql_connect("localhost","peter","abc123"); 
if (!$con) 
{ 
    die(&#39;Could not connect: &#39; . mysql_error()); 
} 
mysql_select_db("my_db", $con); 
mysql_query("DELETE FROM Persons WHERE LastName=&#39;Griffin&#39;"); 
mysql_close($con); 
?>
Copy after login

例子,批量删除,批量删除文章这个技术没什么高深莫测的,只是想写下来与大家分享,适合初学者:

1、首先在文章列表页面(list.php),将多选筐命名为:“$del_id[]”,值为文章ID号,例如(list.php),代码如下:

<form name="del_form" action="del.php" method="post">  
<?php  
    $result=mysql_query("select * from news");  
    while($rs=mysql_fetch_array($result)){  
 ?>  
    <input name="del_id[]" type="checkbox" id="del_id[]" value="<?=$rs[id]?>" /><?=$rs[title]?>  
 <?php  
    }  
 ?>  
</form>
Copy after login

2、处理页面del.php,代码如下:

<?php  
if($del_id!=""){  
    $del_num=count($del_id);  
    for($i=0;$i<$del_num;$i++){  
        mysql_query("Delete from news where id=&#39;$del_id[$i]&#39;");  
    }   
    echo("<script type=&#39;text/javascript&#39;>alert(&#39;删除成功!&#39;);history.back();</script>");  
}else{  
    echo("<script type=&#39;text/javascript&#39;>alert(&#39;请先选择项目!&#39;);history.back();</script>");  
}  
?>
Copy after login

注释:SQL 对大小写不敏感,DELETE FROM 与 delete from 等效,为了让 PHP 执行上面的语句,我们必须使用 mysql_query函数,该函数用于向 SQL 连接发送查询和命令.


source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template