index.php
<?php
$servername = "localhost";
$username = "root";
$password = "133nubia022";
$dbname = "myweb";
// 创建连接
$conn = mysqli_connect($servername, $username, $password,$dbname);
$sql = "select * from user";
$rst = mysqli_query($conn,$sql);
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
*{
font-family: 微软雅黑;
}
</style>
<link rel="stylesheet" href="bs/css/bootstrap.min.css">
<script src='bs/js/jquery.min.js'></script>
<script src='bs/js/bootstrap.min.js'></script>
</head>
<body>
<div class="container">
<h2 class='page-header'>查看用户:</h2>
<table class='table table-bordered table-hover'>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>删除</th>
</tr>
<?php
while($row=mysqli_fetch_assoc($rst)){
echo "<tr>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['usename']}</td>";
echo "<td>{$row['password']}</td>";
echo "<td><a href='javascript:' class='del' id='{$row['id']}'>删除</a></td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
<script>
$('.del').click(function(){
id=this.id;
obj=$(this);
$.getJSON('http://localhost:8888?cb=?',{id:id},function(json){
console.warn(json);
if(json.ok=='1'){
obj.parent().parent().hide(1000);
}
});
});
</script>
</html>
node安装mysql
在D:\xampp\htdocs\node\node
npm install mysql
index.js
http = require('http');
url = require('url');
querystring = require('querystring');
mysql = require('mysql');
cs = function(req, res) {
uri = req.url;
json = querystring.parse(url.parse(uri).query);
fname = json.cb;
id = json.id;
jsonstr = fname + '({"ok":"1"})';
//连接、操作数据库
conn = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '133nubia022',
database: 'myweb'
});
// 操作数据库
conn.connect();
conn.query('delete from user where id=' + id, function(err, rs) {
if (rs.affectedRows == '1') {
//给前端jsonp返回成功删除的消息{ok:1}
res.write(jsonstr);
//关闭响应
res.end();
}
});
//关闭数据库
conn.end();
}
http.createServer(cs).listen(8888);
**
**
**
**
**
**
**
**
**
**
**
**
**