增删改查是对数据库最基本的操作,下面详细为大家介绍下如何连接数据库以及增删改查等等相关知识,感兴趣的朋友可以参考下
1.链接数据库通用方法:conn.php
复制代码 代码如下:
//第一步:链接数据库
$conn=@mysql_connect("localhost:3306","root","root")or die ("mysql链接失败");
//第二步: 选择指定的数据库,设置字符集
@mysql_select_db("php_blog",$conn) or die ("db链接失败".mysql_error());
mysql_query('SET NAMES UTF8')or die ("字符集设置错误");
?>
复制代码 代码如下:
include("conn.php");//引入链接数据库
if(!empty($_POST['sub'])){
$title=$_POST['title'];
$con=$_POST['con'];
echo $sql="insert into news(id,title,dates,contents) value (null,'$title',now(),'$con')" ;
mysql_query($sql);
echo"插入成功";
}
?>
复制代码 代码如下:
include("conn.php");// 引入链接数据库
<?php <BR>include("conn.php");// 引入链接数据库链接数据库 <br>if(!empty ($_GET['id'])){ <br>$sql="select * from news whereid']."'"; <br>$query=mysql_query($sql) ; <br>$rs=mysql_fetch_array($query) <br>} <br>if(!empty($_POST['sub'])){ <br>$title=$_POST['title']; 🎜>$con=$_POST['con'] <br>$hid=$_POST['hid']; <br>$sql="更新新闻集 title='$title',contents='$con'其中 limit 1 "; <br>mysql_query($sql); <br>echo "<script>; alert('更新成功'); location.href='index.php'</script>"; <br>echo"更新成功"; <br>} <br>?> <br>;
<?php <BR>include("conn.php");//导入链接数据库 <BR>if(!empty ($_GET['id'])){ <BR>$sql="从新闻中选择 * whereid']."'"; <BR>$query=mysql_query($sql); <BR>$rs=mysql_fetch_array($query); <BR>} <BR>if(!empty($_POST['sub'])){ <BR>$title=$_POST['title']; <BR>$con=$_POST['con']; <BR>$hid=$_POST['hid']; <BR>$sql="更新新闻集 title='$title',contents='$con' where limit 1 "; <BR>mysql_query($sql); <BR>echo "<script>alert('更新成功'); location.href='index.php'</script>"; <BR>echo"更新成功"; <BR>} <BR>?> <BR><form action="edit.php" method="post"> <br><input type="hidden" value="<?php echo $rs['id']?>"/> <br>标题:<input type="text" value="<?php echo $rs['title']?>"><br> <BR>内容:<textarea rows=" cols="50"><?php echo $rs['contents']?><br>; <br> <br></form>
<a href="add.php">添加内容</a> <br><hr> <br><hr> <br><形式> <br> <br><input type="submit" value="搜索"/> <br></表格> <br><?php <BR>include("conn.php");//导入链接数据库 <br>if(!empty($_GET['keys'])){ <br>$w=" title像 '%".$_GET['keys']."%'"; <br>}其他{ <br>$w=1; <br>} <br>$sql="select * from news where $w order by id desc"; <br>$query=mysql_query($sql); <br>while($rs=mysql_fetch_array($query)){ <br>?>; <br><h2>标题:<a href="view.php?id=<?php echo $rs['id'] ?>"><?php echo $rs['title' ] ?></a> <a href="edit.php?id=<?php echo $rs['id'] ?>">编辑</a>||<a href="del.php?del= <?php echo $rs['id'] ?>">删除</a></h2> <br><li><?php echo $rs['dates'] ?></li> <br><p><?php echo $rs['contents'] ?></p> <br><hr> <br><?php <BR>} <br>?> <br>