Blogger Information
Blog 29
fans 0
comment 0
visits 27245
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
使用MySQLi面向过程的方式,实现三种方式的数据库连接操作
LIWEN的博客
Original
758 people have browsed it

使用MySQLi面向过程的方式,连接数据库:

1、使用函数mysqli_connect(hostname,username,password,db_name);

2、主机名:localhost,或者127.0.0.1

3、用户名和密码:php工具箱,都是root

4、db_name,既可以在连接的时候给出,也可以在连接成功后单独设置。

三种方式实现代码如下:

<?php
header('Content-Type:text/html;charest=utf-8');

//第一种数据库连接方式
$db = mysqli_connect('localhost','root','root','test');
if (!$db){
    echo '连接失败'.mysqli_connect_error();
} else {
    echo '连接成功<br>';
}
mysqli_set_charset($db,'utf8');

//第二种数据库连接方式:定义常量来替换mysqli_connect()函数的参数
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','root');
define('DB_NAME','test');
define('DB_CHAR','utf8');

$db2 = mysqli_connect(DB_HOST,DB_USER,DB_PASS);
if (!$db2){
    echo '连接失败'.mysqli_connect_error();
} else {
    echo '连接db2成功<br>';
}
mysqli_select_db($db2,DB_NAME);
mysqli_set_charset($db2,DB_CHAR);

//第三种数据库连接方式,实际应用中最多的方式
define('DB_PASS','root1');   //将密码改为错误的
$db3 = @mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME) or die('db3连接失败'.mysqli_connect_error());
mysqli_set_charset($db3,DB_CHAR);


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