try / catch or INSERT INTO... ON DUPLICATE KEY UPDATE [message #175294] |
Thu, 01 September 2011 03:50 |
jwcarlton
Messages: 76 Registered: December 2010
Karma: 0
|
Member |
|
|
I promise that this is a PHP question, and not MySQL!
I'm currently sending a query of INSERT INTO... ON DUPLICATE KEY
UPDATE. But, I'd like to know which one happened in my PHP; was it
new, or was it updated?
Is there a way to find this? Or should I do something like:
try {
$query = sprintf("INSERT...");
$sth = mysql_query($query) or die(mysql_error());
$duplicate = false;
}
catch (Exception $e) {
$query = sprintf("UPDATE...");
$sth = mysql_query($query) or die(mysql_error());
$duplicate = true;
}
|
|
|
Re: try / catch or INSERT INTO... ON DUPLICATE KEY UPDATE [message #175295 is a reply to message #175294] |
Thu, 01 September 2011 04:37 |
Chuck Anderson
Messages: 63 Registered: September 2010
Karma: 0
|
Member |
|
|
jwcarlton wrote:
> I promise that this is a PHP question, and not MySQL!
>
> I'm currently sending a query of INSERT INTO... ON DUPLICATE KEY
> UPDATE. But, I'd like to know which one happened in my PHP; was it
> new, or was it updated?
>
> Is there a way to find this? Or should I do something like:
>
> try {
> $query = sprintf("INSERT...");
> $sth = mysql_query($query) or die(mysql_error());
>
> $duplicate = false;
> }
>
> catch (Exception $e) {
> $query = sprintf("UPDATE...");
> $sth = mysql_query($query) or die(mysql_error());
>
> $duplicate = true;
> }
>
>
Your promise is an empty one. It's very much MySQL, but I'll give it to
ya anyway: :-)
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
..... right there in the first several lines ...
"With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if
the row is inserted as a new row and 2 if an existing row is updated."
Php -> mysql_affected_rows($link)
--
*****************************
Chuck Anderson • Boulder, CO
http://cycletourist.com
Turn Off, Tune Out, Drop In
*****************************
|
|
|
Re: try / catch or INSERT INTO... ON DUPLICATE KEY UPDATE [message #175296 is a reply to message #175295] |
Thu, 01 September 2011 05:35 |
jwcarlton
Messages: 76 Registered: December 2010
Karma: 0
|
Member |
|
|
On Sep 1, 12:37 am, Chuck Anderson <cycletour...@invalid.invalid>
wrote:
> jwcarlton wrote:
>> I promise that this is a PHP question, and not MySQL!
>
>> I'm currently sending a query of INSERT INTO... ON DUPLICATE KEY
>> UPDATE. But, I'd like to know which one happened in my PHP; was it
>> new, or was it updated?
>
>> Is there a way to find this? Or should I do something like:
>
>> try {
>> $query = sprintf("INSERT...");
>> $sth = mysql_query($query) or die(mysql_error());
>
>> $duplicate = false;
>> }
>
>> catch (Exception $e) {
>> $query = sprintf("UPDATE...");
>> $sth = mysql_query($query) or die(mysql_error());
>
>> $duplicate = true;
>> }
>
> Your promise is an empty one. It's very much MySQL, but I'll give it to
> ya anyway: :-)
>
> http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
>
> .... right there in the first several lines ...
>
> "With ON DUPLICATE KEY UPDATE, the affected-rows value per row is 1 if
> the row is inserted as a new row and 2 if an existing row is updated."
>
> Php -> mysql_affected_rows($link)
>
> --
> *****************************
> Chuck Anderson • Boulder, CO
> http://cycletourist.com
> Turn Off, Tune Out, Drop In
> *****************************
Touche! Thanks, Chuck. It's obvious once you know what to look for :-)
|
|
|