search for in the  
<mysql_get_server_infomysql_insert_id>
Last updated: Thu, 19 May 2005

mysql_info

(PHP 4 >= 4.3.0, PHP 5)

mysql_info -- Get information about the most recent query

Description

string mysql_info ( [resource link_identifier] )

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 by chance no connection is found or established, an E_WARNING level warning 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 aren't 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.



User Contributed Notes
mysql_info
eric at projectsatellite dot com
22-Sep-2003 04:54
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);
}
carl at NOSPAMthep dot lu dot se
06-Sep-2002 06:44
This function can be used as a workaround for a misfeature of MySQL: on an UPDATE, rows that aren't updated _solely because they looked the same before_ will not be seen in mysql_affected_rows(). This causes problems when you want to use the result of the update to determine if there's need to do an INSERT. With MySQL you can do an INSERT IGNORE if there's no risk of if failing because of a duplicate key other than the one used in the UPDATE. However, if this isn't the case or you want a bit of RDBMS independence, there's no easy/pretty workaround. I think I'll resort to doing a SELECT to determine the primary key before doing the update/insert, as using the CVS version of PHP isn't an option for me.

<mysql_get_server_infomysql_insert_id>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT