Re: MySQL: affected_rows treibt mich in den Wahnsinn
Martin Knipper <[email protected]>
| Newsgroups | gmane.linux.suse.programming |
|---|---|
| Message-ID | <[email protected]> |
Am Montag, 29. November 2004 22:03 schrieb Joerg Rossdeutscher (Joerg Rossdeutscher <[email protected]>): > > UPDATE SET name="ratti" WHERE id=13 > if mysql_affected_rows == 0 { > INSERT SET name="ratti" , id=13 > } > > > Das klappt in allen Fällen - AUSSER, wenn der Datensatz vorher > schon genau so aussah, denn affected_rows meldet nur die > tatsächlich veränderten Datensätze zurück (Bin ich alleine mit > der Meinung, daß das total bescheuert ist?). In diesem Fall > bekomme ich eine 0 geliefert, der nächste Befehl liefert mir dann > einen Fehler, weil die id als UNIQUE deklariert ist - was auch > korrekt ist. > Habe folgendes für Dich gefunden: (vgl: http://de2.php.net/manual/de/function.mysql-affected-rows.php) "Note: When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possiblity that mysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query." As of PHP 4.3.0 (I assume, I only tried with 4.3.2), you can make mysql_affected_rows() return the number of rows matched, even if none are updated. You do this by setting the CLIENT_FOUND_ROWS flag in mysql_connect(). For some reason, not all the flags are defined in PHP, but you can use the decimal equivalent, which for CLIENT_FOUND_ROWS is 2. So, for example: $db= mysql_connect("localhost", "user", "pass", false, 2); mysql_select_db("mydb", $db); $query= "UPDATE ..."; mysql_query($query); print mysql_affected_rows(); // more than 0 mysql_query($query); // same query twice print mysql_affected_rows(); // still more than 0 Gruß Martin -- Um die Liste abzubestellen, schicken Sie eine Mail an: [email protected] Um eine Liste aller verfügbaren Kommandos zu bekommen, schicken Sie eine Mail an: [email protected]