<?php
/**
* Created by PhpStorm.
* User: 程冬
* Date: 2017/7/24
* Time: 10:25
*/
///**
// * 面向对象化的方法
// */
//$mysqli = mysqli_init();
//if (!$mysqli) {
// die('mysqli_init failed');
//}
//
//if (!$mysqli->options(MYSQLI_INIT_COMMAND, 'SET AUTOCOMMIT = 0')) {
// die('Setting MYSQLI_INIT_COMMAND failed');
//}
//
//if (!$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5)) {
// die('Setting MYSQLI_OPT_CONNECT_TIMEOUT failed');
//}
//
//if (!$mysqli->real_connect('localhost', 'root', '', 'school')) {
// die('Connect Error (' . mysqli_connect_errno() . ') '
// . mysqli_connect_error());
//}
//
//if ($mysqli->connect_errno) {
// printf("Connect failed: %s
", $mysqli->connect_error);
// exit();
//}
//
///* Create table doesn't return a resultset */
//if ($mysqli->query("CREATE TEMPORARY TABLE myuser LIKE user") === TRUE) {
// printf("Table myuser successfully created.
");
//}
//
///* Select queries return a resultset */
//if ($result = $mysqli->query("SELECT userName FROM user LIMIT 10")) {
// printf("Select returned %d rows.
", $result->num_rows);
//
// /* free result set */
// $result->close();
//}
//
///* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
//if ($result = $mysqli->query("SELECT * FROM user", MYSQLI_USE_RESULT)) {
//
// /* Note, that we can't execute any functions which interact with the
// server until result set was closed. All calls will return an
// 'out of sync' error */
//// if (!$mysqli->query("SET @a:='this will not work'")) {
//// printf("Error: %s
", $mysqli->error);
//// }
// $result->close();
//}
////echo 'Success... ' . $mysqli->host_info . "
";
//
//$mysqli->close();
//$link=mysqli_real_connect('127.0.0.1:3306','root','') or exit('连接失败...');
//mysqli_select_db('pg39',$link);
//$infos = mysqli_query('select * from student',$link);
//
//$arr=array();
//if($infos){
// while($info = mysqli_fetch_array($infos,MYSQL_BOTH)){
// array_push($arr,$info);
// }
//}
//
//mysqli_close($link);
/**
* 连接数据库的mysqli函数库
* 使用的是过程化的风格编码
*/
// 获取数据库的连接
$link=mysqli_init();
// 判断mysqli对象是否被创建成功
if(!$link){
die('mysqli_init 创建失败...');
}
// 设置mysqli的参数值,这里是设置是否自动提交SQL语句
if(!mysqli_options($link,MYSQLI_INIT_COMMAND,'SET AUTOCOMMIT = 0')){
die('设置自动提交失败...');
}
// 设置连接的实效的时间,设置为5秒
if(!mysqli_options($link,MYSQLI_OPT_CONNECT_TIMEOUT,5)){
die('设置连接的时间失败...');
}
// 创建mysqli连接数据库
if(!mysqli_real_connect($link,'localhost','root','','school')){
die('连接出现了错误('.mysqli_connect_errno().')');
mysqli_connect_error();
}
// 定义SQL语句并发送到数据库端执行操作且返回结果
if(!mysqli_set_charset($link,'utf8')){
die('设置数据库查询编码集失败...');
}
$infos=mysqli_query($link,'select * from user',MYSQLI_USE_RESULT);
// 处理返回结果
$arr=array();
if($infos){
while($info = mysqli_fetch_array($infos,MYSQLI_NUM)){
array_push($arr,$info);
}
// 处理结束将获取到的数据集合清除
mysqli_free_result($infos);
}
var_dump($arr);
// 关闭打开的资源
mysqli_close($link);