很多站长朋友们都不太清楚phpmysqli用法,今天小编就来给大家整理phpmysqli用法,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php中mysqli处理查询结果集的几个方法 2、 thinkphp怎么使用mysqli扩展库 3、 php使用mysqli向数据库添加数据的方法 4、 php mysqli 常用函数有哪些 5、 php中使用mysqli创建数据库的时候怎么指定字符集和排序规则? 6、 php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例 php中mysqli处理查询结果集的几个方法$sql="select * from user"; $result=$link->query($sql); $row=$result->fetch_all(MYSQLI_BOTH);//参数MYSQL_ASSOC、MYSQLI_NUM、MYSQLI_BOTH规定产生数组类型
$n=0; while($n<mysqli_num_rows($result)){ echo "ID:".$row[$n]["id"]."用户名:".$row[$n]["name"]."密码:".$row[$n]["password"]."<br />"; $n++;
}
thinkphp怎么使用mysqli扩展库这个在配置里面是可以设置的,一般是与连接数据库设置一起配置:
//数据库配置信息
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'thinkphp', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '123456', // 密码
'DB_PORT' => 3306, // 端口
'DB_PREFIX' => 'think_', // 数据库表前缀
'DB_CHARSET'=> 'utf8', // 字符集
'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志
其中数据库类型可以设置为:mysqli
更多类型还有:
DB_TYPE设置
支持的 数据库类型
mysql或mysqli mysql
pgsql pgsql
sqlite sqlite
mssql 或sqlsrv sqlserver
oracle oracle
ibase ibase
mongo mongo
PDO PDO支持的所有数据库
php使用mysqli向数据库添加数据的方法本文实例讲述了php使用mysqli向数据库添加数据的方法。分享给大家供大家参考。具体实现方法如下:
$mydb
=
new
mysqli('localhost',
'username',
'password',
'databasename');
$sql
=
"INSERT
INTO
users
(fname,
lname,
comments)
VALUES
('$_POST[fname]',
'$_POST[lname]',
'$_POST[comments]')";
if
($mydb->query($sql)
==
TRUE)
{
echo
"user
entry
saved
successfully.";
}
else
{
echo
"INSERT
attempt
failed"
;
}
$mydb->close();
希望本文所述对大家的php程序设计有所帮助。
php mysqli 常用函数有哪些php 中mysqli是个类,这个类的函数(方法)有:
mysqli::$affected_rows — Gets the number of affected rows in a previous MySQL operation
mysqli::autocommit — 打开或关闭本次数据库连接的自动命令提交事务模式
mysqli::begin_transaction — Starts a transaction
mysqli::change_user — Changes the user of the specified database connection
mysqli::character_set_name — 返回当前数据库连接的默认字符编码
mysqli::$client_info — Get MySQL client info
mysqli::$client_version — Returns the MySQL client version as a string
mysqli::close — 关闭先前打开的数据库连接
mysqli::commit — 提交一个事务
mysqli::$connect_errno — Returns the error code from last connect call
mysqli::$connect_error — Returns a string description of the last connect error
mysqli::__construct — Open a new connection to the MySQL server
mysqli::debug — Performs debugging operations
mysqli::dump_debug_info — 将调试信息输出到日志
mysqli::errno — 返回最近函数调用的错误代码
mysqli::$error_list — Returns a list of errors from the last command executed
mysqli::$error — Returns a string description of the last error
mysqli::$field_count — Returns the number of columns for the most recent query
mysqli::get_charset — Returns a character set object
mysqli::get_client_info — Get MySQL client info
mysqli_get_client_stats — Returns client per-process statistics
mysqli_get_client_version — 作为一个整数返回MySQL客户端的版本
mysqli::get_connection_stats — Returns statistics about the client connection
mysqli::$host_info — 返回一个表述使用的连接类型的字符串
mysqli::$protocol_version — 返回MySQL使用的协议版本号
mysqli::$server_info — 返回MySQL服务器的版本号
mysqli::$server_version — 作为一个整数返回MySQL服务器的版本
mysqli::get_warnings — Get result of SHOW WARNINGS
mysqli::$info — Retrieves information about the most recently executed query
mysqli::init — Initializes MySQLi and returns a resource for use with mysqli_real_connect()
mysqli::$insert_id — Returns the auto generated id used in the last query
mysqli::kill — Asks the server to kill a MySQL thread
mysqli::more_results — Check if there are any more query results from a multi query
mysqli::multi_query — Performs a query on the database
mysqli::next_result — Prepare next result from multi_query
mysqli::options — Set options
mysqli::ping — Pings a server connection, or tries to reconnect if the connection has gone down
mysqli::poll — Poll connections
mysqli::prepare — Prepare an SQL statement for execution
mysqli::query — 对数据库执行一次查询
mysqli::real_connect — 建立一个 MySQL 服务器连接
mysqli::real_escape_string — Escapes special characters in a string for use in an SQL statement, taking into account the current charset of the connection
mysqli::real_query — 执行一个mysql查询
mysqli::reap_async_query — Get result from async query
mysqli::refresh — Refreshes
mysqli::release_savepoint — Removes the named savepoint from the set of savepoints of the current transaction
mysqli::rollback — 回退当前事务
mysqli::rpl_query_type — Returns RPL query type
mysqli::savepoint — Set a named transaction savepoint
mysqli::select_db — 选择用于数据库查询的默认数据库
mysqli::send_query — 发送请求并返回结果
mysqli::set_charset — 设置默认字符编码
mysqli::set_local_infile_default — Unsets user defined handler for load local infile command
mysqli::set_local_infile_handler — Set callback function for LOAD DATA LOCAL INFILE command
mysqli::$sqlstate — Returns the SQLSTATE error from previous MySQL operation
mysqli::ssl_set — Used for establishing secure connections using SSL
mysqli::stat — Gets the current system status
mysqli::stmt_init — 初始化一条语句并返回一个用于mysqli_stmt_prepare(调用)的对象
mysqli::store_result — Transfers a result set from the last query
mysqli::$thread_id — Returns the thread ID for the current connection
mysqli::thread_safe — 返回是否是线程安全的
mysqli::use_result — Initiate a result set retrieval
mysqli::$warning_count — Returns the number of warnings from the last query for the given link
以上函数清单直接来自网站。你可以进入该网站参看。
php中使用mysqli创建数据库的时候怎么指定字符集和排序规则?字符集很简单,但是数据的排序需要通过SQL语句来协助完成,ORDER BY 语句,代码如下:
// 假设你已经成功连接了数据库($mysqli变量假设为连接的资源句柄)
// 通过对象方式设置字符编码
$mysqli -> set_charset('utf8');
// 通过函数方式设置字符编码
mysqli_set_charset($mysqli, 'utf8');
// 那么接下来是数据排序的话,需要编写一条SQL查询语句(DESC 倒序排列 | ASC 正序排列)
$sql = "SELECT `字段` FROM `表名` WHERE TRUE ORDER BY `字段` DESC;";
如果还有什么问题,欢迎追问~
php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例本文实例讲述了php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率。分享给大家供大家参考,具体如下:
<?php
/**
*
测试pdo和mysqli的执行效率
*/
header("Content-type:text/html;charset=utf-8");
//通过pdo链接数据库
$pdo_startTime
=
microtime(true);
$pdo
=
new
PDO("mysql:host=localhost;dbname=test","root","1234",array(PDO::MYSQL_ATTR_INIT_COMMAND
=>
"SET
NAMES'utf8';"));
for($i=1;$i<=100;$i++){
$title
=
"pdo标题".$i;
$content
=
"pdo内容".$i;
$addtime
=
time();
$user_id
=
$i;
$pdo_sql
=
"INSERT
INTO
`article`(`title`,`content`,`addtime`,`user_id`)
VALUES(:title,:content,:addtime,:user_id)";
$sth
=
$pdo->prepare($pdo_sql);
$sth->bindParam(':title',$title);
$sth->bindParam(':content',$content);
$sth->bindParam(':addtime',$addtime);
$sth->bindParam(':user_id',$user_id);
$sth->execute();
}
$pdo_endTime
=
microtime(true);
$pdo_time
=
$pdo_endTime
-
$pdo_startTime;
echo
$pdo_time;
echo
"<hr/>";
//通过mysql链接数据库
$mysqli_startTime
=
microtime(true);
$mysqli
=
mysqli_connect("localhost","root","1234","test")
or
die("数据连接失败");
mysqli_query($mysqli,"set
names
utf8");
for($i=1;$i<=100;$i++){
$title
=
"mysqli标题".$i;
$content
=
"mysqli内容".$i;
$addtime
=
time();
$user_id
=
$i;
$sql
=
"INSERT
INTO
`article`(`title`,`content`,`addtime`,`user_id`)
VALUES('".$title."','".$content."',".$addtime.",".$user_id.")";
mysqli_query($mysqli,$sql);
}
$mysqli_endTime
=
microtime(true);
$mysqli_time
=
$mysqli_endTime
-
$mysqli_startTime;
echo
$mysqli_time;
echo
"<hr/>";
if($pdo_time
>
$mysqli_time){
echo
"pdo的执行时间是mysqli的".round($pdo_time/$mysqli_time)."倍";
}else{
echo
"mysqli的执行时间是pdo的".round($mysqli_time/$pdo_time)."倍";
}
测试结果:其实经过多次测试,pdo和mysqli的执行效率差不多。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:php使用mysqli和pdo扩展,测试对比连接mysql数据库的效率完整示例php中数据库连接方式pdo和mysqli对比分析php中关于mysqli和mysql区别的一些知识点分析php操作mysqli(示例代码)php封装的mysqli类完整实例PHP以mysqli方式连接类完整代码实例php简单解析mysqli查询结果的方法(2种方法)php中mysql连接方式PDO使用详解Php中用PDO查询Mysql来避免SQL注入风险的方法php
mysql
PDO
查询操作的实例详解PHP实现PDO的mysql数据库操作类
关于phpmysqli用法的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于phpmysqli用法 常用的php操作mysql的函数有哪些的详细内容...