mysql-insert-id

getting mysql_insert_id() while using ON DUPLICATE KEY UPDATE with PHP

谁都会走 提交于 2019-12-17 15:36:22
问题 I've found a few answers for this using mySQL alone, but I was hoping someone could show me a way to get the ID of the last inserted or updated row of a mysql DB when using PHP to handle the inserts/updates. Currently I have something like this, where column3 is a unique key, and there's also an id column that's an autoincremented primary key: $query ="INSERT INTO TABLE (column1, column2, column3) VALUES (value1, value2, value3) ON DUPLICATE KEY UPDATE SET column1=value1, column2=value2,

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement'

元气小坏坏 提交于 2019-12-17 05:12:54
问题 When I run the following code, I get the error saying Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'No index used in query/prepared statement' $mysql = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database'); if (mysqli_connect_errno()) { printf("DB error: %s", mysqli_connect_error()); exit(); } $get_emp_list = $mysql->prepare("SELECT id, name FROM calc"); if(!$get_emp_list){ echo "prepare failed\n"; echo "error: ",

2nd query fails to run

别说谁变了你拦得住时间么 提交于 2019-12-14 03:22:18
问题 I'm new to programming and I have a problem with the following code. The 2nd query is not running. It should insert all the data in the first database to the other database. MySQLConn = New MySqlConnection MySQLConn.ConnectionString = Connection Adapter = New MySqlDataAdapter Dim QRY = "SELECT EquipmentID, Quantity FROM subdbborroweq" Dim EQID As Integer Dim QTY As Integer Dim TimeAndDate As String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") Try MySQLConn.Open() Command = New MySqlCommand

Alternative to “PDO::lastInsertId” / “mysql_insert_id”

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 08:04:45
问题 I always hear that using "lastInsertId" (or mysql_insert_id() if you're not using PDO) is evil. In case of triggers it obviously is, because it could return something that's totally not the last ID that your INSERT created. $DB->exec("INSERT INTO example (column1) VALUES ('test')"); // Usually returns your newly created ID. // However when a TRIGGER inserts into another table with auto-increment: // -> Returns newly created ID of trigger's INSERT $id = $DB->lastInsertId(); What's the

MYSQL get auto increment/mysql_insert_id() at INSERT

帅比萌擦擦* 提交于 2019-12-12 04:53:38
问题 I would like to get auto_incr value of new entry while performing INSERT, and create the table field 'url' on the fly. Example: database table fields: id,category,sex,age,url (i want to insert a url with the auto_incr at the end) variables: $category = employee; $sex = male; $age = 30; INSERT INTO table VALUES(NULL,$category,$sex,$age,'mywebsite.com/employee-male-30-00000001') note: assuming the newly inserted id is 00000001 I am currently inserting the new entry with a blank url, and then

Best way to find the last inserted ID in mysql using php

 ̄綄美尐妖づ 提交于 2019-12-03 15:57:44
Im wondering as to what the best solution is to get the last inserted ID after a mysql inquiry? I have found the following solutions : <?php function get_current_insert_id($table) { $q = "SELECT LAST_INSERT_ID() FROM $table"; return mysql_num_rows(mysql_query($q)) + 1; } ?> or even using mysql_insert_id php function, but apparently this function will not work well with bigint (thats what I am using for ID field) and if there are alot of consecutive sql inquiries it could be unreliable. Could someone provide a reliable and fast solution to achieve this task? Isn't SELECT LAST_INSERT_ID()

Alternative to “PDO::lastInsertId” / “mysql_insert_id”

你说的曾经没有我的故事 提交于 2019-12-03 14:36:41
I always hear that using "lastInsertId" (or mysql_insert_id() if you're not using PDO) is evil. In case of triggers it obviously is, because it could return something that's totally not the last ID that your INSERT created. $DB->exec("INSERT INTO example (column1) VALUES ('test')"); // Usually returns your newly created ID. // However when a TRIGGER inserts into another table with auto-increment: // -> Returns newly created ID of trigger's INSERT $id = $DB->lastInsertId(); What's the alternative? If you go the route of ADOdb ( http://adodb.sourceforge.net/ ), then you can create the insert ID

can I trust mysql_insert_id() to return correct value, multithreading paranoia

和自甴很熟 提交于 2019-12-02 09:14:24
问题 I made a function that inserts a new line into a table, after calling an INSERT statement into mysql, I then make some checks, and then I check the value of mysql_insert_id() . I want to be quiet knowing that it is not possible for another thread to break in, inserting a row, before I get the result of mysql_insert_id() . My bet is that it should be safe, but I am asking because this one can be disastrous. 回答1: From the manual: The ID that was generated is maintained in the server on a per

can I trust mysql_insert_id() to return correct value, multithreading paranoia

自古美人都是妖i 提交于 2019-12-02 04:29:07
I made a function that inserts a new line into a table, after calling an INSERT statement into mysql, I then make some checks, and then I check the value of mysql_insert_id() . I want to be quiet knowing that it is not possible for another thread to break in, inserting a row, before I get the result of mysql_insert_id() . My bet is that it should be safe, but I am asking because this one can be disastrous. From the manual : The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO

How bad is using SELECT MAX(id) in MYSQL instead of mysql_insert_id() in PHP?

馋奶兔 提交于 2019-11-30 18:00:50
Background: I'm working on a system where the developers seem to be using a function which executes a MYSQL query like "SELECT MAX(id) AS id FROM TABLE" whenever they need to get the id of the LAST inserted row (the table having an auto_increment column). I know this is a horrible practice (because concurrent requests will mess the records), and I'm trying to communicate that to the non-tech / management team, to which their response is... "Oh okay, we'll only face this problem when we have (a) a lot of users, or (b) it'll only happen when two people try doing something at _exactly_ the same