Blogger Information
Blog 22
fans 0
comment 0
visits 17899
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysqli面向过程连接数据库三种方式
yestrue的博客
Original
963 people have browsed it

config.php 文件

<?php

define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_NAME','test');
define('DB_CHAR','utf8');

connect.php 文件

<?php
include 'config.php';
header('content-type:text/html; charset=utf-8');
// 面向过程连接数据库,方式一
// $db = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD);
// if(!$db){
// 	echo '连接失败'.mysqli_connect_error();
// }else{
// 	echo '<h3>连接成功</h3>';
// }
// mysqli_select_db($db,DB_NAME);
// mysqli_set_charset($db,DB_CHAR);
// 面向过程连接数据库,方式二
// $db = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
// if(!$db){
// 	echo '连接失败'.mysqli_connect_error();
// }else{
// 	echo '<h3>连接成功</h3>';
// }
// mysqli_set_charset($db,DB_CHAR);
// 面向过程连接数据库,方式三
$db = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME) or die('连接失败'.mysqli_connect_error());
echo '<h3>连接成功</h3>';
mysqli_set_charset($db,DB_CHAR);
//操作
$sql = 'SELECT `id`,`name`,`email` FROM user';
$res1 = mysqli_query($db,$sql);
if($res1 && mysqli_num_rows($res1)>0){
	$res[] = mysqli_fetch_all($res1,MYSQLI_ASSOC); 

}else{
	echo '数据为空';
}
echo '<pre>';
print_r($res);

//释放结果集
mysqli_free_result($res1);
//关闭连接
mysqli_close($db);


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