whrTC

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

<?php
    $link = mysqli_connect(\'localhost\',\'root\',\'\');
    var_dump($link);
    //1、连接数据库
    if (!$link) {
    exit(\'连接数据库失败\');}
    //2、判断数据库是否连接成功
    mysqli_set_charset($link,\'utf8\');
    //3、设置字符集
    mysqli_select_db($link,\'bbs\');
    //4、选择数据库
    $sql = "select * from bbs_user";
    //5、准备sql语句
    $res = mysqli_query($link,$sql);
    //6、发送sql语句
    $result = mysqli_fetch_assoc($res);
    $result = mysqli_fetch_assoc($res);
    //7、处理结果集
    mysqli_close($link);
    //8、关闭数据库
?>

这个返回的是一个关联的数组。

 

 

输出全部数组:(用循环)

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

 

 

输出一个索引的数组:

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

 

即输出关联数组,又输出索引数组:

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

 

查询数据总数:

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

 

用php插入新的数据:

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

 

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

 

对删除php文件进行编译:(del.php)

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

对修改php文件进行编译:(update.php)

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

doupdate.php:

1 <?php
2     var_dump($_GRT);
3 ?>

doupadate.php

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

 

分类:

技术点:

相关文章:

  • 2021-11-17
  • 2021-09-12
  • 2021-12-13
  • 2021-11-17
  • 2021-11-17
  • 2021-12-10
  • 2021-12-03
猜你喜欢
  • 2021-11-17
  • 2021-11-17
  • 2021-04-26
  • 2021-08-16
  • 2021-11-17
  • 2021-12-26
  • 2021-05-06
相关资源
相似解决方案