PHP Conference Japan 2024

mysql_info

(PHP 4 >= 4.3.0, PHP 5)

mysql_info取得最近查詢的資訊

警告

這個擴充功能在 PHP 5.5.0 中已被棄用,並在 PHP 7.0.0 中移除。取而代之,應該使用 MySQLiPDO_MySQL 擴充功能。另請參閱 MySQL:選擇 API 指南。此函式的替代方案包括

描述

mysql_info(resource $link_identifier = NULL): string

返回關於上次查詢的詳細資訊。

參數

link_identifier

MySQL 連線。如果沒有指定連線識別符,則會假設為 mysql_connect() 最後開啟的連線。如果沒有找到這樣的連線,它會嘗試建立一個連線,如同呼叫 mysql_connect() 時沒有帶任何引數。如果找不到或建立連線,則會產生 E_WARNING 級別的錯誤。

回傳值

成功時返回關於該陳述式的資訊,失敗時返回 false。請參閱下面的範例,了解哪些陳述式會提供資訊,以及傳回的值可能的外觀。未列出的陳述式將返回 false

範例

範例 #1 相關的 MySQL 陳述式

返回字串值的陳述式。這些數字僅用於說明目的;它們的值將對應於查詢。

INSERT INTO ... SELECT ...
String format: Records: 23 Duplicates: 0 Warnings: 0 
INSERT INTO ... VALUES (...),(...),(...)...
String format: Records: 37 Duplicates: 0 Warnings: 0 
LOAD DATA INFILE ...
String format: Records: 42 Deleted: 0 Skipped: 0 Warnings: 0 
ALTER TABLE
String format: Records: 60 Duplicates: 0 Warnings: 0 
UPDATE
String format: Rows matched: 65 Changed: 65 Warnings: 0

註解

註解:

mysql_info() 只有在陳述式中指定多個值列表時,才會為 INSERT ... VALUES 陳述式傳回非 false 值。

參見

新增註解

使用者貢獻的註解 4 則註解

8
info at granville dot nl
19 年前
我快速轉換了 eric 的函式,只是為了計算查詢中匹配或影響的列數。

/**GD gdf_db_count_query_v1:傳回上次查詢匹配或影響的列數。必須在相關查詢後立即使用。
*/

function gdf_db_count_query($link = 'dbh') {

$info_str = mysql_info($$link);

if (ereg("Records: ([0-9]*)", $info_str, $count) == false) {
ereg("Rows matched: ([0-9]*)", $info_str, $count);
}

return $count;

}
6
eric at projectsatellite dot com
21 年前
我同意這是一個有用的函式,當嘗試檢查更新查詢是否匹配特定列時使用。我建立了一個簡單的函式,該函式會傳回一個帶有傳回字串中劃分的值的關聯陣列。

function get_mysql_info($linkid = null){
$linkid? $strInfo = mysql_info($linkid) : $strInfo = mysql_info();

$return = array();
ereg("Records: ([0-9]*)", $strInfo, $records);
ereg("Duplicates: ([0-9]*)", $strInfo, $dupes);
ereg("Warnings: ([0-9]*)", $strInfo, $warnings);
ereg("Deleted: ([0-9]*)", $strInfo, $deleted);
ereg("Skipped: ([0-9]*)", $strInfo, $skipped);
ereg("Rows matched: ([0-9]*)", $strInfo, $rows_matched);
ereg("Changed: ([0-9]*)", $strInfo, $changed);

$return['records'] = $records[1];
$return['duplicates'] = $dupes[1];
$return['warnings'] = $warnings[1];
$return['deleted'] = $deleted[1];
$return['skipped'] = $skipped[1];
$return['rows_matched'] = $rows_matched[1];
$return['changed'] = $changed[1];

return $return;
}

在嘗試更新可能存在或不存在的列之後,您可以使用上述函式,如下所示

$vals = get_mysql_info($linkid);
if($vals['rows_matched'] == 0){
mysql_query("INSERT INTO table values('val1','val2', 'valetc')", $linkid);
}
4
tomas at matfyz dot cz
15 年前
請注意,由於 mysql 錯誤 #41283 和 #41285,無法從 mysql_info() 取得警告計數的資訊

http://bugs.mysql.com/?id=41283
http://bugs.mysql.com/?id=41285
1
bdobrica at gmail dot com
18 年前
針對先前貼文中指出 mysql_affected_rows() 在您進行更新查詢時,即使查詢有效,但欄位未修改時會傳回 0 的問題,我張貼了以下函式。它非常簡單,並基於先前的貼文。

function mysql_modified_rows () {
$info_str = mysql_info();
$a_rows = mysql_affected_rows();
ereg("Rows matched: ([0-9]*)", $info_str, $r_matched);
return ($a_rows < 1)?($r_matched[1]?$r_matched[1]:0):$a_rows;
}

希望您會覺得它有用。
To Top