Blogger Information
Blog 29
fans 0
comment 0
visits 29290
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Mysqli面向对象-连接
咸鱼梦
Original
732 people have browsed it

Mysqli面向对象-连接

config.php文件:

<?php
header( 'Content-Type:text/html;charset=utf-8 ');
//创建连接参数: 因为连接参数不会经常变化,所以推荐使用常量
define ('DB_HOST', 'localhost');
define ('DB_USER', 'root');
define ('DB_PASS', 'root');
define ('DB_NAME', 'demo');
define ('DB_CHAR', 'utf8');

connect.php文件:

<?php
/**
 * 连接操作
 * 1. mysqli面向对象,第一步必须要创建一个mysqli对象
 * 2. 连接操作涉及到mysqli对象的二个方法与二个属性
 * 3. 方法: $mysqli->select_db(), $mysqli->set_charset()
 * 4. 属性: $mysqli->connect_errno, $mysqli->connect_error()
 */
require 'config.php';  //导入连接的配置文件

$mysqli = new mysqli(DB_HOST,DB_USER, DB_PASS);
if ($mysqli->connect_errno) {  //检测是否有连接错误代码
    printf("Connect failed: %s\n", mysqli_connect_error());  //返回文本描述的连接错误代码
    exit();
}
$mysqli->select_db(DB_NAME);  //设置默认打开的数据库
$mysqli->set_charset(DB_CHAR);  //设置客户端默认字符编码集

/**
 * 简化建议:
 * 1. 可以在初始化mysqli对象时,给它的构造器传入要使用的数据库名称
 * 2.$mysqli = new mysqli(DB_HOST,DB_USER, DB_PASS, DB_NAME);
 * 3.这样,后面的$mysqli->select_db(DB_NAME);可以省略
 */


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