PHP链接MySQL数据库
2015-06-13 17:18
465 查看
1.链接数据库:
2.查询:
3.增加:
4.删除:
5.更新:
<?php $conn = @mysql_connect("localhost", "-u", "-p") or die("数据库链接错误"); mysql_select_db("数据库名", $conn); mysql_query("set names 'UTF8'"); ?>
2.查询:
<?php include("connection.php"); $username=$_COOKIE['username']; $sql="select * from message where user='$username'"; $query=mysql_query($sql); while($row=mysql_fetch_array($query)){} ?>
3.增加:
<?php include("connection.php"); if(isset($_POST['submit'])) { $sql="insert into message(id,user,title,content,lastdate) values('','$_COOKIE[username]','$_POST[title]','$_POST[content]',now())"; mysql_query($sql); if(mysql_affected_rows($conn)) echo "<script>alert('插入成功!');</script>"; else echo "<script>alert('插入失败!');</script>"; } ?>
4.删除:
<?php include("connection.php"); if(isset($_POST['tid'])) { $delete_sql = "delete from message where id =$_POST[tid]"; mysql_query($delete_sql); if(mysql_affected_rows($conn)) echo "<script>alert('删除成功!'););location.href='listone.php';</script>"; else echo "<script>alert('删除失败!'););location.href='listone.php';</script>"; } ?>
5.更新:
<?php include("connection.php"); if(isset($_POST['submit'])) { $sql="update message set title='$_POST[title]',content='$_POST[content]',lastdate=now() where id =$_POST[tid]"; mysql_query($sql); if(mysql_affected_rows($conn)) echo "<script>alert('更新成功!');</script>"; else echo "<script>alert('更新失败!');</script>"; } ?>
相关文章推荐
- itextpdf 应用
- oltp与olap区别
- thinkphp3.1.3导入
- Sublime Text 2 – SFTP/FTP 安装与使用
- PHP5.3.3添加安装mcrypt模块
- PHP的几种设计模式
- 关于php面向对象继承方面的几点总结
- PHP获取数组的键与值方法小结
- 自己写RTPserver——大约RTP协议
- 用PHP扩展做一个HelloWorld!
- php编译器
- php7的安装教程
- vsftp的设置选项
- iptables 下开放ftp
- thinkphp的全局变量在js中使用
- php分10个不同等级压缩优化图片(PNG)
- 常用的php ADODB使用方法集锦
- phpPHP创建创建jpg格式图片以及压缩图片(转)
- matplotlib画图异常--超出最大递归次数解决办法
- PHP 开发者应了解的24个库