mysqli_stmt::$insert_id is a property provided by PHP's mysqli extension. After executing an INSERT statement, it returns the AUTO_INCREMENT value of the newly inserted record. This value is often used as the basis for generating unique identifiers. Note that $insert_id is only valid after executing an INSERT statement.
For example, suppose we have an orders table with an auto-increment field order_id that stores the unique identifier for each order. When a new order is inserted, $insert_id will return the order_id of that order.
<span><span><span class="hljs-variable">$stmt</span></span><span> = </span><span><span class="hljs-variable">$mysqli</span></span><span>-></span><span><span class="hljs-title function_ invoke__">prepare</span></span><span>(</span><span><span class="hljs-string">"INSERT INTO orders (product_name, quantity, price) VALUES (?, ?, ?)"</span></span><span>);
</span><span><span class="hljs-variable">$stmt</span></span><span>-></span><span><span class="hljs-title function_ invoke__">bind_param</span></span>(</span><span><span class="hljs-string">"sid"</span></span><span>, </span><span><span class="hljs-variable">$product_name</span></span><span>, </span><span><span class="hljs-variable">$quantity</span></span><span>, </span><span><span class="hljs-variable">$price</span></span><span>);
</span><span><span class="hljs-variable">$stmt</span></span><span>-></span><span><span class="hljs-title function_ invoke__">execute</span></span><span>();
<p></span>$order_id = $stmt->insert_id; </span>// Get the newly inserted order ID<br>
echo "New Order ID: " . $order_id;<br>
</span>
In the snippet above, we execute an order insertion and retrieve the newly inserted order_id using $stmt->insert_id.
Although the AUTO_INCREMENT value itself is a unique integer identifier, sometimes we want a more readable or business-specific identifier, such as including a timestamp or product category. In such cases, we can combine insert_id with other data to generate order numbers or other unique identifiers with business meaning.
Here are some common methods for generating order numbers:
We can combine insert_id with the current timestamp to create a more unique order number. This ensures uniqueness while also embedding time information, making future queries and sorting easier.
<span><span><span class="hljs-variable">$order_id</span></span><span> = </span><span><span class="hljs-variable">$stmt</span></span><span>->insert_id;
</span><span><span class="hljs-variable">$timestamp</span></span><span> = </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">"YmdHis"</span></span><span>); </span><span><span class="hljs-comment">// Current timestamp in format: YYYYMMDDHHMMSS</span></span><span>
</span><span><span class="hljs-variable">$unique_order_id</span></span><span> = </span><span><span class="hljs-string">"ORD"</span></span><span> . </span><span><span class="hljs-variable">$timestamp</span></span><span> . </span><span><span class="hljs-title function_ invoke__">str_pad</span></span><span>(</span><span><span class="hljs-variable">$order_id</span></span><span>, </span><span><span class="hljs-number">5</span></span><span>, </span><span><span class="hljs-string">'0'</span></span><span>, STR_PAD_LEFT);
<p></span>echo "Generated Order Number: " . $unique_order_id;<br>
</span>
In this example, the order number format is ORD20230714153001234, where 20230714153001 is the current timestamp and 234 is the unique order ID obtained from insert_id.
If the application requires associating order numbers with product categories, we can combine product information with insert_id to generate order numbers. For example, an e-commerce platform might use the product category code as a prefix in the order number.
<span><span><span class="hljs-variable">$product_category_code</span></span><span> = </span><span><span class="hljs-string">"ELEC"</span></span>; </span><span><span class="hljs-comment">// Assuming electronics category</span></span><span>
</span><span><span class="hljs-variable">$order_id</span></span><span> = </span><span><span class="hljs-variable">$stmt</span></span>->insert_id;
</span><span><span class="hljs-variable">$unique_order_id</span></span><span> = </span><span><span class="hljs-variable">$product_category_code</span></span><span> . </span><span><span class="hljs-string">'-'</span></span><span> . </span><span><span class="hljs-title function_ invoke__">str_pad</span></span><span>(</span><span><span class="hljs-variable">$order_id</span></span><span>, </span><span><span class="hljs-number">6</span></span><span>, </span><span><span class="hljs-string">'0'</span></span><span>, STR_PAD_LEFT);
<p></span>echo "Generated Order Number: " . $unique_order_id;<br>
</span>
With this method, an order number might look like ELEC-000023, where ELEC represents the product category, and 000023 is the unique identifier generated based on insert_id.
When using mysqli_stmt::$insert_id, in addition to ensuring uniqueness, keep the following key points in mind:
When using transactions, insert_id may return the ID of the last successfully inserted record within the transaction. If multiple INSERT statements are included in a transaction, make sure to use $insert_id at the correct point. If the transaction is rolled back, the insert_id value will also be reset.
While insert_id can generate unique identifiers, in some cases it’s important to ensure unpredictability. For example, avoid using purely numeric identifiers as order numbers to reduce the risk of malicious guessing. Combining dates, timestamps, and other elements can increase the complexity and security of identifiers.
If the AUTO_INCREMENT value in a table is manually reset (e.g., by an administrator using ALTER TABLE), it can cause insert_id to reset or produce duplicates. Therefore, maintaining the health of the table and avoiding unnecessary operations is crucial.
Related Tags:
mysqli_stmt