Blogger Information
Blog 60
fans 0
comment 1
visits 37609
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库操作-4月23日
威灵仙的博客
Original
532 people have browsed it

实例

一、数据库操作

//1.创建连接参数

define('DB_HOST', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASS', 'root');
define('DB_NAME', 'php');
define('DB_CHAR', 'utf8');

//2.调用连接函数mysqli_connect()返回连接对象

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASS);

//3.判断是否连接成功

if(mysqli_connect_errno($dbc)){
   exit('连接失败'.mysqli_connect_error($dbc));
}

//4.选择默认的数据库

mysqli_select_db($dbc, DB_NAME);

//5.设置客户端默认字符编码集

mysqli_set_charset($dbc, DB_CHAR);


/////////////////////////////////////////////////////////////

2、数据库查询的基本步骤mysqli_query()

//1.连接数据库

require 'mysqli_connect.php';

//2.编写查询sql语句

$sql = "SELECT * FROM user;";

//3.执行查询并判断结果

if($res = mysqli_query($dbc, $sql)){
   while($row = mysqli_fetch_array($res, MYSQLI_ASSOC)){
       echo '<pre>';
       var_export($row);
       print '<hr>';
    }
   }else{
   exit('查询失败'.mysqli_errno($dbc).':'.mysqli_connect_error($dbc));
}

//4.释放结果集

mysqli_free_result($res);

//5.关闭连接

mysqli_close($dbc);

运行实例 »

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


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