mysql_info
(PHP 4 >= 4.3.0, PHP 5)
mysql_info — Get information about the most recent query
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
Description
$link_identifier
= NULL
] )Returns detailed information about the last query.
Parameters
-
link_identifier
-
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an
E_WARNING
level error is generated.
Return Values
Returns information about the statement on success, or FALSE
on
failure. See the example below for which statements provide information,
and what the returned value may look like. Statements that are not listed
will return FALSE
.
Examples
Example #1 Relevant MySQL Statements
Statements that return string values. The numbers are only for illustrating purpose; their values will correspond to the query.
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
Notes
Note:
mysql_info() returns a non-
FALSE
value for the INSERT ... VALUES statement only if multiple value lists are specified in the statement.
See Also
- mysql_affected_rows() - Get number of affected rows in previous MySQL operation
- mysql_insert_id() - Get the ID generated in the last query
- mysql_stat() - Get current system status
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MySQL Drivers and Plugins
- Оригинальное API MySQL
- mysql_affected_rows
- mysql_client_encoding
- mysql_close
- mysql_connect
- mysql_create_db
- mysql_data_seek
- mysql_db_name
- mysql_db_query
- mysql_drop_db
- mysql_errno
- mysql_error
- mysql_escape_string
- mysql_fetch_array
- mysql_fetch_assoc
- mysql_fetch_field
- mysql_fetch_lengths
- mysql_fetch_object
- mysql_fetch_row
- mysql_field_flags
- mysql_field_len
- mysql_field_name
- mysql_field_seek
- mysql_field_table
- mysql_field_type
- mysql_free_result
- mysql_get_client_info
- mysql_get_host_info
- mysql_get_proto_info
- mysql_get_server_info
- mysql_info
- mysql_insert_id
- mysql_list_dbs
- mysql_list_fields
- mysql_list_processes
- mysql_list_tables
- mysql_num_fields
- mysql_num_rows
- mysql_pconnect
- mysql_ping
- mysql_query
- mysql_real_escape_string
- mysql_result
- mysql_select_db
- mysql_set_charset
- mysql_stat
- mysql_tablename
- mysql_thread_id
- mysql_unbuffered_query
Коментарии
I agree that this is a useful function to use when trying to check on whether an update query matched a particular row. I created a simple function that returns an associative array with the values delineated in the returned string.
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;
}
After trying to update a row that may or may not exist, you can use the above function like so:
$vals = get_mysql_info($linkid);
if($vals['rows_matched'] == 0){
mysql_query("INSERT INTO table values('val1','val2', 'valetc')", $linkid);
}
Imade a quick conversion of eric's function just to count matched or affected rows from a query.
/**GD gdf_db_count_query_v1: returns the amount of rows matched or affected by the last query. Must be used immediately after the concerned query.
*/
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;
}
As a solution to the problem pointed in the post reffering to mysql_affected_rows() returning 0 when you are making an update query and the fields are not modified although the query is valid, i'm posting the following function. It is very simple and based on a previous post.
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;
}
Hope you'll find it usefull.
Please note that the information on warning count cannot be taken from the mysql_info() due to mysql bugs #41283 and #41285:
http://bugs.mysql.com/?id=41283
http://bugs.mysql.com/?id=41285