跟着咖啡学习php(四)---php操作数据库

我们今天聊下php怎么连接mysql数据库。PHP+MYSQL作web应用可谓是黄金搭档,我们这里演示的案例采用的是mysqli这个mysql的扩展函数。

mysqli函数库和mysql函数库的应用基本类似。

1.连接mysql服务器
这里写图片描述

示例:

<?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $conn = mysqli_connect($host,$username,$password,"bolg");
    if($conn){
         echo "数据库连接成功";
    }else{
        echo "数据库连接失败";
    }

你在地址栏中输入相对应的路径后,页面会输出”数据库连接成功”。
由于这个连接数据库是普遍性的,那么我们会将mysql服务器连接会存储在一个目录conn文件夹下,文件名称为conn.php。

2.  执行SQL语句
要对数据库中的表进行操作,通常使用mysqli_query()函数执行SQL语句。
mysqli_query( mysqli link, string query [, int resultmode] )

link:必选参数,mysqli_connect()函数成功连接MySql数据库服务器所返回的连接标识符。
query:必选参数,索要执行的查询语句。

我们来看下简单的sql执行语句,

执行一个插入语句
$result = mysqli_query($conn,"insert into tb_member values('qaq','123','[email protected]')");
设置数据库编码格式
mysqli_query($conn,"set names utf8");

3.将结果返回数组中
使用mysqli_query()函数执行select语句,如果成功,将返回查询结果集。下面介绍一个对查询结果集进行操作的函数,mysqli_fetch_array()。它把结构返回数组中。

mysqli_fetch_array(resource result,unt result_type)

result:资源类型的参数,要传入的是由mysqli_query()函数返回的数据指针。
result_type:可选项,设置结果集数组的表述方式。有以下3中取值

mysqli_assic:返回一个关联数组。数组下标由表的字段名组成
mysqli_num:返回一个索引数组。数组下标由数字组成
mysqli_both:返回一个同时包含关联和数字索引的数组。默认值为mysqli_both
注意:本函数返回的字段名区分大小写。
<?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $conn = mysqli_connect($host,$username,$password,"bolg");
    mysqli_query($conn,"set names utf8");//设置编码格式
    if(!$conn){
         echo "数据库连接失败";
    }
    $result = mysqli_query($conn,"select * from blog_content");
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="css/bootstrap.css" />
    </head>
    <body>
        <div class="container">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h4>表格查询</h4>
                </div>
                <div class="panel-body">
                    <table class="table table-bordered text-center table-hover">
                        <tr>
                            <td>编号</td>
                            <td>内容</td>
                            <td>描述</td>
                            <td>时间</td>
                        </tr>
                        <?php
                            while($row = mysqli_fetch_array($result)){
                        ?><tr>
                            <td><?php echo $row[0];?></td>
                            <td><?php echo $row[1];?></td>
                            <td><?php echo $row[2];?></td>
                            <td><?php echo $row[3];?></td>
                        </tr>
                        <?php
                            }
                        ?>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>

4.从结果集中获取一行作为对象

mysqli_fetch_object(resource result)
mysqli_fetch_object()函数和mysqli_fetch_array()函数类似,只有一点区别,就是它返回的是一个对象而不是数组
即该函数只能通过字段名访问数组
示例:$row->id和$row->name来访问行中的元素值
本字段同样区分大小写
<?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $conn = mysqli_connect($host,$username,$password,"bolg");
    mysqli_query($conn,"set names utf8");//设置编码格式
    if(!$conn){
         echo "数据库连接失败";
    }
    $result = mysqli_query($conn,"select * from blog_content");
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="css/bootstrap.css" />
    </head>
    <body>
        <div class="container">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h4>表格查询</h4>
                </div>
                <div class="panel-body">
                    <table class="table table-bordered text-center table-hover">
                        <tr>
                            <td>编号</td>
                            <td>内容</td>
                            <td>描述</td>
                            <td>时间</td>
                        </tr>
                        <?php
                            while($row = mysqli_fetch_object($result)){
                        ?><tr>
                            <td><?php echo $row->h_title;?></td>
                            <td><?php echo $row->h_message;?></td>
                            <td><?php echo $row->h_img;?></td>
                            <td><?php echo $row->h_time;?></td>
                        </tr>
                        <?php
                            }
                        ?>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>

5.从结果集中获取一行作为枚举数组

mysqli_fetch_row(resource result)函数用于从结果集中取得一行作为枚举数组,如果没有更多行则返回null。返回数组
的偏移量从0开始,即以$row[0]的形式访问第一个元素(只有一个元素也是如此)

6.从结果集中获取一行作为关联数组

mysqli_fetch_assoc()函数返回根据所取得的行生成的数组,如果没有更多行则返回null。该数组下标为数据表中字段
的名称
<?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $conn = mysqli_connect($host,$username,$password,"bolg");
    mysqli_query($conn,"set names utf8");//设置编码格式
    if(!$conn){
         echo "数据库连接失败";
    }
    $result = mysqli_query($conn,"select * from blog_content");
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="css/bootstrap.css" />
    </head>
    <body>
        <div class="container">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h4>表格查询</h4>
                </div>
                <div class="panel-body">
                    <table class="table table-bordered text-center table-hover">
                        <tr>
                            <td>编号</td>
                            <td>内容</td>
                            <td>描述</td>
                            <td>时间</td>
                        </tr>
                        <?php
                            while($row = mysqli_fetch_assoc($result)){
                        ?><tr>
                            <td><?php echo $row["h_title"];?></td>
                            <td><?php echo $row["h_message"];?></td>
                            <td><?php echo $row["h_img"];?></td>
                            <td><?php echo $row["h_time"];?></td>
                        </tr>
                        <?php
                            }
                        ?>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>

7.获取查询结果集中的记录数

使用mysqli_num_rows()函数,可以获取由select语句查询到的结果集中行的数目。mysqli_num_rows()函数的语法如下。
int mysql_num_rows(resource result)
mysql_num_rows()返回结果集中行的数目。
<?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $conn = mysqli_connect($host,$username,$password,"bolg");
    mysqli_query($conn,"set names utf8");//设置编码格式
    if(!$conn){
         echo "数据库连接失败";
    }
    $result = mysqli_query($conn,"select * from blog_content");
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="css/bootstrap.css" />
    </head>
    <body>
        <div class="container">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h4>表格查询
                        共有<?php
                            $nums = mysqli_num_rows($result);
                            echo $nums."条数据";
                        ?>
                    </h4>
                </div>
                <div class="panel-body">
                    <table class="table table-bordered text-center table-hover">
                        <tr>
                            <td>编号</td>
                            <td>内容</td>
                            <td>描述</td>
                            <td>时间</td>
                        </tr>
                        <?php
                            while($row = mysqli_fetch_assoc($result)){
                        ?><tr>
                            <td><?php echo $row["h_title"];?></td>
                            <td><?php echo $row["h_message"];?></td>
                            <td><?php echo $row["h_img"];?></td>
                            <td><?php echo $row["h_time"];?></td>
                        </tr>
                        <?php
                            }
                        ?>
                    </table>
                </div>
            </div>
        </div>
    </body>
</html>

猜你喜欢

转载自blog.csdn.net/Only_ruiwen/article/details/81668790