问题描述:

I'm very new to MySQL and have a table (listOfNames) with 3 columns like so:

id[int] auto-increment (primary)

name[varchar] (unique)

timesSeen[int] default 1

I insert "name" into the table by first checking if a row for a name already exists, if not we add it, else we update timesSeen by increasing it's value by 1. I'm able to do that with the following php mysqli objects:

$query = "INSERT INTO `listOfNames`(`name`) VALUES (`someName`) ON DUPLICATE KEY UPDATE `timesSeen` = VALUES (`timesSeen`) + 1";

$results = $conn->query($query);

What I want to do is now access the new value of timesSeen for someName without making a new SELECT query. I would think you could access this row and it's values after just inserting or updating it without having to make a new separate SELECT query. However $conn->query() only returns a bool for INSERT queries.

So what is the easiest way to now get the new value of timesSeen for "someName" immediately after my INSERT UPDATE query? Is there some sort of lastResult() member function for my $conn object I'm not seeing?

Thanks for any help you can provide.

网友答案:

There is no way to immediately retrieve the new value without another query. You will always have to send a SELECT-query to retrieve the new value.

相关阅读:
Top