Blogger Information
Blog 30
fans 0
comment 0
visits 18234
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
4.23 数据连接和查询的基本操作
宋的博客
Original
643 people have browsed it

实例

<?php 

//链接数据库mysql
//1.创建连接参数另外文件

define('DB_HOST', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASS', 'root');
define('DB_NAME', 'PHP');
define('DB_CHAR', 'UTF8');



 ?>

运行实例 »

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

实例

<?php

require 'config.php';

//2.导入数据链接函数,成功ture失败false

$db = @mysqli_connect(DB_HOST,DB_USER,DB_PASS);

//3.测试数据库链接是否成功

if (mysqli_connect_errno($db)) {
	exit('链接失败'.mysqli_connect_errno($db));
}
// echo '链接成功';
//4. 选择要操作的数据库
mysqli_select_db($db, DB_NAME);

//5. 设置默认字符集
mysqli_set_charset($db, DB_CHAR);

运行实例 »

实例

<?php 

//单条查询
require 'connect.php';

//查询语句
$sql = "select name from staff";

//执行查询,成功返回结果,失败返回失败原因

$result = mysqli_query($db,$sql);

	if (mysqli_affected_rows($db) > 0 ) {
		echo '查询到'.mysqli_affected_rows($db).'条数据';
} else {
	echo '没有查询到数据';
}

while ($row = mysqli_fetch_object($result )){
	var_export($row);
	echo '<br>';
}
//释放结果集(仅针对select)
mysqli_free_result($result);

//关闭数据库连接
mysqli_close($db);


 ?>

运行实例 »

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

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


运行效果图:

QQ图片20180425000112.png


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