Blogger Information
Blog 42
fans 0
comment 1
visits 25860
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysqli_connect()及mysqli_query()学习-2018年4月24日上午11:27完成
邵军-山东-84918的博客
Original
540 people have browsed it

连接设置:

实例

<?php
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','root');
define('DB_NAME','php');
define('DB_CHAR','utf8');

运行实例 »

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

数据库连接:

实例

<?php
// 入数据库连接参数
require'mysqli_config.php';
// 2. 调用连接函数,成功则返回mysqli对象,失败返回false
$db=@mysqli_connect(DB_HOST,DB_USER,DB_PASS);
// 3. 测试连接是否成功
if(mysqli_connect_errno($db)){
    exit('连接失败'.mysqli_connect_error($db));
}else
{
    echo '连接ok'.'<br>';
}
// 4. 选择要操作的数据库
mysqli_select_db($db,DB_NAME);
// 5. 设置默认字符集
mysqli_set_charset($db,DB_CHAR);

运行实例 »

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

数据库查询操作:

实例

<?php
require'connect.php';
// 执行查询
if($res=mysqli_query($db,'SELECT user_name FROM user')){
while ($row=mysqli_fetch_assoc($res)) {
   var_export($row);
   echo'<hr>';
}
}

else{
    exit('查询失败'.mysqli_errno($db).':'.mysqli_error($db));

}
// 释放结果集(仅针对select)
mysqli_free_result($res);
//4.关闭数据库连接
mysqli_close($db);

运行实例 »

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


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