php如何连接数据库的方法
来源:不言
发布时间:2018-12-05 15:21:31
阅读量:1788
这篇文章给大家介绍的内容是关于php如何连接数据库的方法,有着一定的参考价值,有需要的朋友可以参考一下。
1、php链接数据库:
1、链接数据库
2、判断是否连接成功
3、设置字符集
4、选择数据库
5、准备SQL语句
6、发送SQL语句
7、处理结果集
8、释放资源(关闭数据库)
$result = mysqli_querry($link,$sql) //返回一个对象
mysqli_fetch_assoc($result) 一个一个往下读,返回的时候一个一维的关联数组
mysqli_fetch_row($result) 返回一个索引数组
mysqli_fetch_array($result) 返回一个索引又有关联的数组
mysqli_num_rows($result) 返回查询的时候的结果集的总条数
mysqli_affected_rows($link) 返回你修改的,删除,添加的时候受影响的行数
mysqli_insert_id($link) 返回的是你插入的当前的数据的自增的id
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <<?php
$link = mysqli_connect('localhost','root','');
var_dump($link);
if (!$link) {
exit('连接数据库失败');}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'bbs');
$sql = "select * from bbs_user";
$res = mysqli_query($link,$sql);
$result = mysqli_fetch_assoc($res);
$result = mysqli_fetch_assoc($res);
mysqli_close($link);
?>
|
这个返回的是一个关联的数组。



输出全部数组:(用循环)
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$link = mysqli_connect('localhost','root','');
if (!$link) {
exit($'连接数据库失败');}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'bbs');
$sql = "select * from bbs_user";
$res = mysqli_query($link,$sql);
while ($result = mysqli_fetch_assoc($res)) {
var_dump($result);}
mysqli_close($link);
?>
|

输出一个索引的数组:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$link = mysqli_connect('localhost','root','');
if (!$link) {
exit('连接数据库失败');}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'bbs');
$sql = "select * from bbs_user";
$res = mysqli_query($link,$sql);
$result = mysqli_fetch_row($res);
var_dump($result);
mysqli_close($link);
?>
|

即输出关联数组,又输出索引数组:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$link = mysqli_connect('localhost','root','');
if (!$link){
exit('连接数据库失败');}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'bbs');
$sql = "select * from bbs_user";
$res = mysqli_query($link,$sql);
$result = mysqli_fetch_array($res);
var_dump($result);
mysqli_close($link);
?>
|

查询数据总数:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$link = mysqli_connect('localhost','root','');
if (!$link) {
exit('连接数据库失败');}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'bbs');
$sql = "select * from bbs_user";
$obj = mysqli_query($link,$sql);
$res = mysqli_num_rows($obj);
var_dump($res);
mysqli_close($link);
?>
|

用php插入新的数据:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$link = mysqli_connect('localhost','root','');
if (!$link) {
exit('连接数据库失败');}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'bbs');
$sql = "insert into bbs_user values(9,'kkk','789789','nanjian',2,15)";
$obj = mysqli_query($link,$sql);
$res = mysqli_insert_id($link);
var_dump($res);
mysqli_close($link);
?>
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php
$link = mysqli_connect('lcoalhost','root','');
if (!$link) {
exit('链接数据库失败');}
mysqli_set_charset($link,'utf8';)
mysqli_select_db($link,'bbs');
$sql = "select * from bbs_user";
$obj = mysqli_query($link,$sql);
echo '<th>编号</th><th>用户名</th><th>地址</th><th>性别</th><th>年龄</th>';
while ($res = mysqli_fetch_assoc($obj)) {
echo '<tr>';
echo '<td>'.$res['id'].'</td>';
echo '<td>'.$res['username'].'</td>';
echo '<td>'.$res['address'].'</td>';
echo '<td>'.$res['sex'].'</td>';
echo '<td>'.$res['age'].'</td>';
echo '<td><a href="del.php?id='.$res['id'].'">删除</a>/<a href="update.php?id='.$res['id'].'">修改</a></td>';
echo '</tr>';}
?>
|



对删除php文件进行编译:(del.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php
$id=$_GET['id'];
$link = mysqli_connect('localhost','root','');
if (!$link) {
exit('连接数据库失败');}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'bbs');
$sql = "delete from bbs_user where id=$id";
$boolearn = mysqli_query($link,$sql);
if ($boolearn && msyqli_affected_rows($link)) {
echo '删除成功';} else {
echo '删除失败';}
mysqli_close($link);
?>
|

对修改php文件进行编译:(update.php)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php
$id = $_GET['id'];
$link = mysqli_connect('localhost','root','');
if (!$link) {
exit('连接数据库失败');}
mysqli_set_charset($link,'utf8');
msyqli_select_db($link,'bbs');
$sql = "select * from bbs_user where id=$id";
$obj = mysqli_query($link,$sql);
$rows = mysqli_fetch_assoc($obj);
?>
<html>
<form action =" doupdate.php">
<input type="hidden" value="<?php echo $id;?>" name="id" />
用户名:<input type="text" value="<?php $rows=['username'] ?>" name="username"/><br />
地址:<input type="text" value="<?php $rows=['address'] ?>" name="address" /><br />
性别:<input type="text" value="<?php $rows=['sex'] ?>" name="sex" />
<br />
年龄:<input type="text" value="<?php $row=['age']>" name="age" />
<input type="submit" value="执行修改" />
</form>
</html>
|
doupdate.php:
1 2 3 | 1 <?php
2 var_dump($_GRT);
3 ?>
|

doupadate.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?php
$id = $_GET['id'];
$username = $_GET['username'];
$address = $_GET['adress'];
$sex = $_GET['sex'];
$age = $_GET['age'];
$link = mysqli_connect('lcoalhost','root','');
if (!$link) {
exit('数据库连接失败');}
mysqli_set_charset($link,'utf8');
mysqli_select_db($link,'bbs');
$sql = "update bbs_user set username='$username', address='$address',
sex='$sex', age='$age' where id='$id'";
$res = mysqli_query($link,$sql);
if ($res && mysqli_affected_rows($link)) {
echo '修改成功<a href="update.php">返回</a>';}
else {
echo '修改失败';}
mysqli_close($link);
?>
|


相关推荐:
PHP如何删除目录自定义的函数
如何使用PHP来写一个简单的解释器