Well, here's my problem. I've got a php script which posts a message to a mysql database. The mysql database generates two values automatically, one is a timestamp and the other one is a unique id for the message.
I need those values to be able to update my page using ajax (json). I want to know how do I get those values from the same php script from where i've posted my message to be stored in the mysql database?
mysql_insert_id will give you the last autoincrement value. Then you can use a SELECT to retrieve the timestamp.
You could use mysql_insert_id to retrieve last inserted ID from the table
Related
I understand the lastInsertId or mysqli_insert_id function returns the last inserted id of the insert or update query but say you had two intending users filling out a form in different locations at the same time and user2's id was the last at the time the mysqli_insert_id on user1's script was being run instead of user1's id. will the id returned be user2's id or user1's id? I know the script will run in micro or milliseconds and its possible the scenario might not ever occur but is it possible that the returned id in a case where two intending users post form data at the same time can be different from the actual id that should be returned.
I hope the question is pretty clear. I am also very new to programming so pardon me.I would also like to thank all the contributors on stack exchange, I have learnt alot in a very short while.
Nothing to worry about here.
They all call the underlying MySQL functionality.
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.
You can use LAST_INSERT_ID().
It will get you the latest row inserted in a table i.e the last row for you.
Is their anyway of checking whether data has been entered into the table and then displaying the updated data as soon as it is updated ??
What I can understand is that you are working for a Client-Server Message project.
You need to create a column for the timestamps in MySQL named anything like 'timestamp' and everytime you enter a entry in the table, for example a chat table, you need to insert the timestamp there in the same query.
In php you can use time() to get the current timestamp. You can set your timezone using this : date_default_timezone_set(), PHP timezones.
And save a variable which saves the last time retreived from the database. Send that with the query to the server.
Return the client all the messages based on the timestamp.
Use mysqli_error() to check, if this returns false you know the INSERT ran as you intended.
mysqli_query($conn,"INSERT INTO mytable (`columnone`,`columntwo`,`columnthree`)
VALUES ('$valueone','$valuetwo','$valuethree')")
or die(mysqli_error($db));
Now run a SELECT query to find the item you just inserted, and return it. Simple!
If you want all this to happen without reloading the page, use AJAX and return the result of the SELECT query upon successful submission.
If I understand you correctly I think you need to store a timestamp for every update and insert (or one for both). Just add a column to each table (I usually call it 'ts'). You can even make it update automatically: https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
Edit: then you can obviously use that column to select the most recently updated row, see if anything's changed in X amount of time etc etc
I have the following query:
UPDATE myTable SET myTime=utc_timestamp() WHERE myID=something
I would like to retrieve the timestamp that was set, preferably within the same query of with something like mysql_insert_id().
Thank you all !
First, mysql_insert_id() is doing a new query. Why you want the data returned you currently saved? Then you might be able to access via the Datacontainer/Variable you got it from.
mysql_insert_id() is just returning the last inserted id of a table. Its not delivering any more data.
To really answer your question, you should tell what you want to do first.
IMPORTANT: mysql_insert_id() wont return you the id of an updated row !
In case you want to get the last inserted id of an updated row - take a look at this answer https://stackoverflow.com/a/14002784/3048505
I am building a web application, which can be used by multiple users simultaneously. They can add data at the same time.
I have a table named doctor_main as follows
Screenshot of DB http://img687.imageshack.us/img687/7033/testxqz.png
Once a record about a doctor is added, I want the id of the inserted record(which is an auto increment field) to be returned.
I know i can use methods like LAST_INSERT_ID() and mysql_insert_id. But i don't know how it behaves.
I need the exact id of the record which is inserted by that particular user.
Sometimes, if two users are trying to insert a record, the id which is returned shouldn't get exchanged.
To achieve this what kind of mysql function should i use ?
There's a whole page in the manual dedicated to this subject:
How to Get the Unique ID for the Last Inserted Row
Here's a quote from that page that should help answer your question:
For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client.
mysql_insert_id() returns exactly the id of the last inserted record, so if you just echo mysql_insert_id() you'll get the id of the very last inserted row.
According to the docs, the mysql_insert_id will return to you the exact id of the insert that you done before.
LAST_INSERT_ID() operates per-connection; multiple users will use multiple connections, so there will never be an exchange of IDs between users.
LAST_INSERT_ID() and mysql_insert_id works fine. Each client will receive the last inserted ID for the last statement that client executed.
If you are suspicious on the mechanism of last_insert_id, then you may assign the id by hand not by auto_increment feature of MySQL.
I was wondering how can I select a value from a database that a user just entered it into and then add it to another mysql table all in the same script before the script is finished running.
You're probably looking for an insert ... select statement.
If you're talking about adding a value that a user just entered into a form, to something, and then putting that into the database, you should do the addition while in PHP. There's no point in going to the database after you've just inserted the value for this purpose.
If I'm misunderstanding something, please elaborate your question and let us know WHY you would want to figure out a just-inserted database value and do an operation on it, rather than trying to do it before you insert in the first place.
Also, if it's a fairly simple modification consider using an UPDATE statement, not a select --> insert.
Like nash said, you perform a select.
But to get the data from the row that the user just entered, you'll need:
mysql_insert_id()
Which grabs the last ID inserted (this is assuming you have an increment id column)
So assuming just entered his first and last name in a form, you'd insert his first and last name in the database(which i assume you know how since the title of this question is "SELECT a value from MySQL database"), you can get what he just entered by:
$last_id = mysql_insert_id();
If there are no rows on that table yet, then this will return 1. $last_id is now 1 (one).
To select:
SELECT * FROM users WHERE userID = "$last_id"
this will grab what the user just inserted....however, this seems pointless as you can use the variables from the form he just filled
enter code here
In the PHP MySQL module, you normally perform a mysql_select_db() to switch database.
You can insert your data into tables in different databases by switching between them with that function.
However, you can insert data into any table of any database (which the user has access to) by prefixing the database name to the table like so:
INSERT INTO test_db.test_table (`column1`,`column2`) VALUES ('abc',123);
You can use that also to insert data from one table into another using:
INSERT INTO `db1`.`myTable` (`column1`,`column2`) SELECT `column1`,`column2` FROM `db2`.`myTable` WHERE `id`= 5
The WHERE id part should obviously match the id of a row in db2.myTable
If you use doctrine you have the inserted data in the object representing the table and in addition you have primary key assigned for the record inside the object.
Con is doctrine is huge database abstraction layer, so if your application is not big doctrine is hammer for mosquito.
what is the structure of your database? The names of your tables, columns?
Some tutorial that you may want to look at: (grabbed from google)
http://www.phpf1.com/tutorial/php-mysql-tutorial.html
In theory you perform a select, take the data you need and perform an insert.