本文目录一览:
php mysqli 常用函数有哪些
php 中 mysqli 是个类,这个类的函数(方法)有:
mysqli::$affected_rows
— Gets the number of affected rows in a previous MySQL operationmysqli::autocommit
— 打开或关闭本次数据库连接的自动命令提交事务模式mysqli::begin_transaction
— Starts a transactionmysqli::change_user
— Changes the user of the specified database connectionmysqli::character_set_name
— 返回当前数据库连接的默认字符编码mysqli::$client_info
— Get MySQL client infomysqli::$client_version
— Returns the MySQL client version as a stringmysqli::close
— 关闭先前打开的数据库连接mysqli::commit
— 提交一个事务mysqli::$connect_errno
— Returns the error code from last connect callmysqli::$connect_error
— Returns a string description of the last connect errormysqli::__construct
— Open a new connection to the MySQL servermysqli::debug
— Performs debugging operationsmysqli::dump_debug_info
— 将调试信息输出到日志mysqli::errno
— 返回最近函数调用的错误代码mysqli::$error_list
— Returns a list of errors from the last command executedmysqli::$error
— Returns a string description of the last errormysqli::$field_count
— Returns the number of columns for the most recent querymysqli::get_charset
— Returns a character set objectmysqli::get_client_info
— Get MySQL client infomysqli_get_client_stats
— Returns client per-process statisticsmysqli_get_client_version
— 作为一个整数返回MySQL客户端的版本mysqli::get_connection_stats
— Returns statistics about the client connectionmysqli::$host_info
— 返回一个表述使用的连接类型的字符串mysqli::$protocol_version
— 返回MySQL使用的协议版本号mysqli::$server_info
— 返回MySQL服务器的版本号mysqli::$server_version
— 作为一个整数返回MySQL服务器的版本mysqli::get_warnings
— Get result of SHOW WARNINGSmysqli::$info
— Retrieves information about the most recently executed querymysqli::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 querymysqli::kill
— Asks the server to kill a MySQL threadmysqli::more_results
— Check if there are any more query results from a multi querymysqli::multi_query
— Performs a query on the databasemysqli::next_result
— Prepare next result from multi_querymysqli::options
— Set optionsmysqli::ping
— Pings a server connection, or tries to reconnect if the connection has gone downmysqli::poll
— Poll connectionsmysqli::prepare
— Prepare an SQL statement for executionmysqli::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 connectionmysqli::real_query
— 执行一个mysql查询mysqli::reap_async_query
— Get result from async querymysqli::refresh
— Refreshesmysqli::release_savepoint
— Removes the named savepoint from the set of savepoints of the current transactionmysqli::rollback
— 回退当前事务mysqli::rpl_query_type
— Returns RPL query typemysqli::savepoint
— Set a named transaction savepointmysqli::select_db
— 选择用于数据库查询的默认数据库mysqli::send_query
— 发送请求并返回结果mysqli::set_charset
— 设置默认字符编码mysqli::set_local_infile_default
— Unsets user defined handler for load local infile commandmysqli::set_local_infile_handler
— Set callback function for LOAD DATA LOCAL INFILE commandmysqli::$sqlstate
— Returns the SQLSTATE error from previous MySQL operationmysqli::ssl_set
— Used for establishing secure connections using SSLmysqli::stat
— Gets the current system statusmysqli::stmt_init
— 初始化一条语句并返回一个用于mysqli_stmt_prepare调用的对象mysqli::store_result
— Transfers a result set from the last querymysqli::$thread_id
— Returns the thread ID for the current connectionmysqli::thread_safe
— 返回是否是线程安全的mysqli::use_result
— Initiate a result set retrievalmysqli::$warning_count
— Returns the number of warnings from the last query for the given link 以上函数清单直接来自网站。你可以进入该网站参看。
关于php操作mysql执行数据库查询的一些常用操作汇总
php操作mysql步骤:
$connect = mysql_connect('localhost','root','123456') or die('数据库连接失败。' . mysql_error());
链接mysql。mysql_select_db('database', $connect)
选择链接的数据库。mysql_query('Set names gb2312'); $sql = "select * from blog_article";
准备要查询的数据。$datas = mysql_query($sql);
执行sql查询。$data = mysql_fetch_assoc($datas);
得到查询到的缓存在内存中的一条数据。print_r($data);
相同点:三个函数都是返回数据库中查询到的一行数据(说的再清楚点就是一条数据)。 不同点:
mysql_fetch_assoc()
用的是数据库中相应的字段名作为的 key 值(也就是数组下标),如:$filed['id']=1;
mysql_fetch_row()
用的是自动生成的数字(从0开始依次生成)作为的 key 值(也就是数组下标),如:$filed[0]=1;
mysql_fetch_array()
用的是自动生成的数字(从0开始依次生成)作为的 key 值(也就是数组下标),而且它还同时生成数据库中相应的字段名作为的 key 值,如:$filed[0]=1
,$filed['id']=1
。也就是说,mysql_fetch_array()
将mysql_fetch_assoc()
和mysql_fetch_row()
查询到的结果合为了一体。mysql_fetch_object()
与mysql_fetch_assoc()
差不多,只是mysql_fetch_assoc()
返回的是数组,mysql_fetch_object()
返回的是 object 对象。 其他常用函数:mysql_insert_id()
取得上一步 INSERT 操作产生的 ID。mysql_result()
函数返回结果集中一个字段的值。mysql_num_fields()
函数返回结果集中字段的数目。mysql_affected_rows();
返回前一次 MySQL 操作所影响的记录行数。mysql_num_rows(mysql_query($sql))
获得结果集中行的数目。mysql_pconnect()
函数打开一个到 MySQL 服务器的持久连接。与mysql_connect()
非常相似,但有两个主要区别:- 当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接。
- 其次,当脚本执行完毕后到 SQL 服务器的连接不会被关闭,此连接将保持打开以备以后使用(
mysql_close()
不会关闭由mysql_pconnect()
建立的连接)。
mysql_data_seek(mysql_query($sql), 8);
获得结果集中的第8条数据。(mysql_num_rows(mysql_query($sql))
和mysql_data_seek(mysql_query($sql), 8)
在mysql_unbuffered_query($sql)
不可以使用。)mysql_unbuffered_query($sql)
和mysql_query($sql)
效果差不多,但是mysql_unbuffered_query($sql)
不缓存,mysql_query($sql)
会缓存查询的结果。mysql_close();
关闭 mysql 的最近的链接。mysql_field_flags(mysql_query($sql), 6)
返回第六个字段的表属性,输出如:not_null primary_key auto_increment。mysql_fetch_lengths(mysql_query($sql))
返回该条数据的所有字段的每个字段的长度,返回的是一个数字组成的数组。mysql_field_name(mysql_query($sql), 3)
返回第三个字段的字段名。mysql_field_table(mysql_query($sql), 0)
返回指定字段所在的表名。mysql_free_result(mysql_query($sql))
函数释放结果内存。mysql_get_client_info()
函数返回 MySQL 客户端信息。mysql_get_host_info()
取得 MySQL 主机信息。
在PHP程序中,执行Mysql命令操作的语句是??
- 第一个函数是链接后台数据库服务。
- 第二个函数是选择数据库。
- 第三个函数是数据迭代器。
- 第四个函数是执行数据库操作语句。
PHP执行批量mysql语句的解决方法
当有多条mysql语句连起来需要执行,比如:
$sqls = "insert into a values(1,2); insert into a values(2,3);";
需要执行的话,PHP中可以使用的方法有三个:
mysql_query
pdo
mysqli
三种方法当 sql 语句没有问题的时候都是可以的。但是当 sql 语句是错误的时候会出现问题:- 第一条 sql 错误:三个方法都返回 false。
- 第一条 sql 正确,第二条 sql 错误:
mysql_query
、pdo
、mysqli::query
也是返回 true。所以这个时候你是没法判断你的 sqls 是否有那条语句是错误的。 解决这种办法有几个:
- 解析 sql 语句
将每条 sql 都拆分开来执行。这样每个语句分开执行就解决了。但是这种方法多出了好几种方法,所以不可取。 - 将 sqls 语句保存为文本
使用 cmd 执行命令mysql ... sqls.sql
,然后捕获输出。这也是一种方法,但是感觉是绕着问题走,应该还有更好的方法。 - 使用 mysqli::multi_query 方法
这个方法可以执行多条 sql 语句,然后使用mysqli::next_result
来设置 sql 的偏移量,使用mysqli::error
获取当前偏移的 sql 的错误状态。 下面是第三种方法的示例代码:
$sql = Config::get('sql');
$content = file_get_contents($sql);
$config = Config::get('config');
$mysqli = mysqli_connect($config['host'], $config['user'], $config['password'], $config['dbname']);
$ret = $mysqli->multi_query($content);
if ($ret === false) {
echo mysqli_error($mysqli);
}
while (mysqli_more_results($mysqli)) {
if (mysqli_next_result($mysqli) === false) {
echo mysqli_error($mysqli);
echo "\r\n";
break;
}
}
$mysqli->close();
这样的话,当 sqls 语句中任意一条有错误的话,程序就会跳出这个错误。如果你要编写初始化 mysql 的脚本的话,这招就非常好用了。