PHP 8.3.4 Released!

mysql_info

(PHP 4 >= 4.3.0, PHP 5)

mysql_infoLit des informations à propos de la dernière requête MySQL

Avertissement

Cette extension était obsolète en PHP 5.5.0, et a été supprimée en PHP 7.0.0. À la place, vous pouvez utiliser l'extension MySQLi ou l'extension PDO_MySQL. Voir aussi MySQL : choisir une API du guide. Alternatives à cette fonction :

Description

mysql_info(resource $link_identifier = NULL): string

Lit des informations à propos de la dernière requête MySQL.

Liste de paramètres

link_identifier

La connexion MySQL. S'il n'est pas spécifié, la dernière connexion ouverte avec la fonction mysql_connect() sera utilisée. Si une telle connexion n'est pas trouvée, la fonction tentera d'ouvrir une connexion, comme si la fonction mysql_connect() avait été appelée sans argument. Si aucune connexion n'est trouvée ou établie, une alerte de niveau E_WARNING sera générée.

Valeurs de retour

Retourne des informations sur une requête en cas de succès, ou false si une erreur survient. Regardez l'exemple ci-dessous pour savoir quelles sortes de requêtes fournissent des informations et avoir un aperçu des informations retournées. Les sortes de requêtes qui ne sont pas listées retournent false.

Exemples

Exemple #1 Requêtes MySQL

Requêtes qui retournent des valeurs. Les nombres ne sont là que pour illustrer l'exemple ; ces valeurs correspondent à la requête.

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() retourne une valeur différente de false pour les commandes INSERT ... VALUES uniquement si plusieurs jeux de valeurs à insérer ont été spécifiés dans la commande.

Voir aussi

add a note

User Contributed Notes 5 notes

up
7
info at granville dot nl
18 years ago
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;

}
up
5
eric at projectsatellite dot com
20 years ago
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);
}
up
4
tomas at matfyz dot cz
15 years ago
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
up
1
bdobrica at gmail dot com
17 years ago
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.
up
-8
carl at NOSPAMthep dot lu dot se
21 years ago
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.
To Top