您的位置:首页 > 编程语言 > PHP开发

PHP链接MySQL数据库

2015-06-13 17:18 465 查看
1.链接数据库:

<?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>";
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: