<?php
/*
* 操作数据库基本步骤
* 1.连接数据库
* 2.操作数据表
* 3.释放结果集:如果不是查询操作,这不可以省略。
* 4.关闭连接
*/
/*
* 连接数据库
* 1.函数mysqli_connect(hostname,username,password,db_name)
* 2.主机名localhost,
* 3.用户名和密码
* 4.db_name,既可以连接时输出,也可以成功后,单独设置
*
*/
//第一种连接:
header('content-type:text/html;charset=utf-8');//设置字符编码
$db = mysqli_connect('localhost','root','root');//把数据库变量值赋给¥db
if(!$db){ //进行判断
echo '连接失败哦'.mysqli_connect_error();//打印出连接失败的信息
}else {
echo '<h1>连接成功</h1>';
}
mysqli_select_db($db,'demo1');
mysqli_set_charset($db, 'utf8');
//第二种:
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','root');
define('DB_NAME','demo1');
define('DB_CHAR','utf8');
$db = mysqli_connect(DB_HOST,DB_USER,DB_PASS, DB_NAME);
if(!$db){ //进行判断
echo '连接失败哦'.mysqli_connect_error();//打印出连接失败的信息
}else {
echo '<h1>连接成功</h1>';
}
mysqli_set_charset($db, 'utf8');
//第三种:
//define('DB_HOST','localhost');
//define('DB_USER','root');
//define('DB_PASS','root');
//define('DB_NAME','demo1');
//define('DB_CHAR','utf8');
$db = mysqli_connect(DB_HOST,DB_USER,DB_PASS, DB_NAME) or die('连接失败'.mysqli_connect_error());
echo '<h1>连接成功</h1>';
mysqli_set_charset($db, 'utf8');
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!