Returns the last autonumber ID field from a previous INSERT query
public GetLastInsertID ( ) : integer | ||
Результат | integer | ID number from previous INSERT query |
/** * @param $data * @return bool|int */ public function membership_role_create($data) { $values = array(); foreach ($data as $key => $value) { if ($key == 'name') { $values[$key] = MySQL::SQLValue($value); } else { $values[$key] = MySQL::SQLValue($value, MySQL::SQLVALUE_NUMBER); } } $table = $this->kga['server_prefix'] . "membershipRoles"; $result = $this->conn->InsertRow($table, $values); if (!$result) { $this->logLastError('membership_role_create'); return false; } return $this->conn->GetLastInsertID(); }
-- -------------------------------------------- */ // --- Open the database -------------------------------------------- // (Also note that you can fill in the variables in the top of the class // if you want to automatically connect when the object is created. If // you fill in the values when you create the obect, this is not needed.) if (!$db->Open("test", "localhost", "root", "password")) { $db->Kill(); } echo "You are connected to the database<br />\n"; // --- Insert a new record ------------------------------------------ $sql = "INSERT INTO Test (Color, Age) Values ('Red', 7)"; if (!$db->Query($sql)) { $db->Kill(); } echo "Last ID inserted was: " . $db->GetLastInsertID(); // --- Or insert a new record with transaction processing ----------- $sql = "INSERT INTO Test (Color, Age) Values ('Blue', 3)"; $db->TransactionBegin(); if ($db->Query($sql)) { $db->TransactionEnd(); echo "Last ID inserted was: " . $db->GetLastInsertID() . "<br /><br />\n"; } else { $db->TransactionRollback(); echo "<p>Query Failed</p>\n"; } // --- Query and show the data -------------------------------------- // (Note: $db->Query also returns the result set) if ($db->Query("SELECT * FROM Test")) { echo $db->GetHTML(); } else {
// $db->ThrowExceptions = true; // ========================================================================= // Example to insert a new row into a table and display it // ========================================================================= // $arrayVariable["column name"] = formatted SQL value $values["Color"] = MySQL::SQLValue("Violet"); $values["Age"] = MySQL::SQLValue(777, MySQL::SQLVALUE_NUMBER); // Execute the insert $result = $db->InsertRow("Test", $values); // If we have an error if (!$result) { // Show the error and kill the script $db->Kill(); } else { // No error, show the new record's ID echo "The new record's ID is: " . $db->GetLastInsertID() . "\n<br />\n"; // Show the record using the values array to generate the WHERE clause // We will use the SelectRows() method to query the database $db->SelectRows("Test", $values); // Show the results in an HTML table echo $db->GetHTML(); } // ========================================================================= // Example to delete a row (or rows) in a table matching a filter // ========================================================================= // Now let's delete that record using the same array for the WHERE clause $db->DeleteRows("Test", $values); // ========================================================================= // Example to update an existing row into a table // ========================================================================= // Create an array that holds the update information