1、引入common.inc.php,这里面包含了很多的操作数据库的方法、连接数据库也做好了,非常方便
require_once (dirname(__FILE__) . "/include/common.inc.php");2、从数据库的表中读取一条数据
$fid = '1'; $row = $dsql->GetOne("SELECT * FROM `dede_feedback` WHERE id='$fid' "); print_r($row) ;3、从数据库的表中读取多条数据
$mid = $_GET['mid']; $equery = "SELECT * FROM `dede_delong` WHERE `mid`='$mid'"; $dsql->SetQuery($equery); $dsql->Execute(); $res = array(); while($dbrow=$dsql->GetObject()){ array_push($res,$dbrow); } echo json_encode($res);4、写入一条到数据库
$res = $dsql->ExecuteNoneQuery("INSERT INTO `dede_delong` (`username`, `position`, `mid`, `lasttime`, `addtime`, `remark`) VALUES ('$username','$position', '$mid', '$lasttime','$addtime','$remark');");5、删除一条记录
$dsql->ExecuteNoneQuery("DELETE FROM `dede_feedback` WHERE `dede_feedback`.`id` = 24");6、删除多条记录
$dsql->ExecuteNoneQuery("DELETE FROM `dede_feedback` WHERE `dede_feedback`.`id` > 30");7、修改一条记录
$dsql->ExecuteNoneQuery("UPDATE `dede_feedback` SET `id` = '129', `aid` = '129', `typeid` = '169', `username` = '你好' WHERE `dede_feedback`.`id` = 29;");普通示例:
<?php require_once (dirname(__FILE__) . "/include/common.inc.php"); ?> <button onclick="onDel()">删除</button> <script type="text/javascript"> function onDel(){ if(confirm('确定删除')){ <?php $dsql->ExecuteNoneQuery("DELETE FROM `dede_feedback` WHERE `dede_feedback`.`id` = 27"); ?> } } </script>ajax示例:
111.php
<script src="http://www.5imoban.net/tpl/js/jquery-3.1.0.min.js"></script> <button onclick="onDel()">ajax异步删除</button> <script type="text/javascript"> function onDel(){ if(confirm('确定删除')){ $.ajax({ //请求方式 type : "GET", //请求的媒体类型 contentType: "application/json;charset=UTF-8", //请求地址 url : "222.php", //数据,json字符串 data : { action:'delete', id:24 }, //请求成功 success : function(result) { alert(result); }, //请求失败,包含具体的错误信息 error : function(e){ console.log(e.status); console.log(e.responseText); } }); } } </script>222.php
<?php require_once (dirname(__FILE__) . "/include/common.inc.php"); $id = $_GET['id']; $action = $_GET['action']; if($action == 'delete'){ $res = $dsql->ExecuteNoneQuery("DELETE FROM `dede_feedback` WHERE `dede_feedback`.`id` = $id"); //$res返回1表示语句执行成功,mysql_affected_rows返回1表示影响了一条记录,返回0表示没有找到要删除的记录 if( $res && mysql_affected_rows() == 1 ){ echo '删除成功'; }else{ echo '删除失败'; } }else{ echo '传参错误!'; } ?>