本文目錄一覽:
- 1、php mysqli 常用函數有哪些
- 2、關於php操作mysql執行數據庫查詢的一些常用操作匯總
- 3、在PHP程序中,執行Mysql命令操作的語句是??
- 4、PHP執行批量mysql語句的解決方法
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操作mysql執行數據庫查詢的一些常用操作匯總
php操作mysql步驟:
1.$connect=mysql_connect(‘localhost’,’root’,’123456′)
or
die(‘數據庫連接失敗。’mysql_error());鏈接mysql。
2.mysql_select_db(‘database’,$connect)選擇鏈接的數據庫。
3.mysql_query(‘Set
names
gb2312′);$sql
=
“select
*
from
blog_article”;準備要查詢的數據。
4.$datas
=
mysql_query($sql);執行sql查詢。
5.$data
=
mysql_fetch_assoc($datas)得到查詢到的緩存在內存中的一條數據。
6.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_pconnect()
和
mysql_connect()
非常相似,但有兩個主要區別:
1.
當連接的時候本函數將先嘗試尋找一個在同一個主機上用同樣的用戶名和密碼已經打開的(持久)連接,如果找到,則返回此連接標識而不打開新連接。
2.
其次,當腳本執行完畢後到
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
table
a
values(1,2);
insert
table
a
values(2,3);」
需要執行的話php中可以使用的方法有三個:
mysql_query
pdo
mysqli
三種方法當sqls語句沒有問題的時候都是可以的。
但是
當sql語句是錯誤的時候會出現問題
第一條sql錯誤:三個方法都返回false
第一條sql正確,第二條sql錯誤:mysql_query、pdo、
mysqli:query也是返回true。所以這個時候你是沒法判斷你的sqls是否有那條語句是錯誤的。
解決這種辦法有幾個招:
1
解析sql語句
將每條sql都拆分開來執行。這樣每個語句分開執行就解決了。但是這種方法多出了好幾種方法,所以不可取。
2
將sqls語句保存為文本
使用cmd執行命令
mysql….
.
sqls.sql,
然後捕獲輸出。這也是一種方法,但是感覺是繞着問題走,應該還有更好的方法。
3
使用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的腳本的話,這招就非常好用了。
原創文章,作者:EZLIO,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/325579.html