Blogger Information
Blog 5
fans 0
comment 0
visits 3028
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4.23作业
Gill的博客
Original
591 people have browsed it

4.23作业

config.php

实例

<?php
//参数
define('DB_HOST','127.0.0.1');//默认数据库服务器地址
define('DB_USER','用户名');//用户名
define('DB_PASS','密码');//密码
define('DB_NAME','数据库名');//数据库名
define('DB_CHAR','字符集');//字符集

运行实例 »

点击 "运行实例" 按钮查看在线实例

connect.php

实例

<?php

//1.创建连接参数
require 'config.php';//引用配置文件
//define('DB_HOST','127.0.0.1');//默认数据库服务器地址
//define('DB_USER','用户名');//用户名
//define('DB_PASS','密码');//密码
//define('DB_NAME','数据库名');//数据库名
//define('DB_CHAR','字符集');//字符集

//2.调用连接函数返回连接对象
$db = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME) or  die('连接失败'.mysqli_connect_error($db));//返回数据库连接对象

//3.判断是否连接成功
//if(mysqli_connect_errno($db)){
//    exit('连接失败'.mysqli_connect_error($db));
//}else {
//    echo '<h1>成功</h1>';
//}

//4.选择默认数据库
//mysqli_select_db(DB_NAME);

//5.选择字符集
mysqli_set_charset(DB_CHAR);

运行实例 »

点击 "运行实例" 按钮查看在线实例

query.php

实例

<?php
//增删改查
//insert/delete/update/select
//mysqli_query($db,$sql);

//1.连接数据库
require 'connect.php';

//2.查询语句
$sql = "select * from DB_NAME";

if($res=mysqli_connect($db,$sql)){
    while ($row=mysqli_fetch_array($res,MYSQL_ASSOC)){
        var_export($row);print '<hr>';
    }
}else {
    exit('查询失败'.mysqli_errno($db).mysqli_connect_error($db));
}

//3.释放结果集
mysqli_free_result($res);

//4.关闭连接
mysqli_close($db);

运行实例 »

点击 "运行实例" 按钮查看在线实例



Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post