This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
Closed 6 years ago.
I tried to find an answer on here already, as well as read the PHP doc on it, however it doesn't explain it very well.
I'm just trying to get the last ID inserted by the user. I know MYSQL is deprecated, but I need to do it this way.
This is what I have so far.
$query="INSERT INTO teams (team_name)
VALUES
('$team_name')";
mysql_query($query) or die('error');
$team_id = mysql_query("SELECT LAST_INSERT_ID()");
Thanks!
Take a look at mysql_insert_id
But beware of the issues when using ON DUPLICATE KEY UPDATE statements. It is described in the comments on the php doc page.
But your solution should work, too.
Also, if you're using mysql, don't forget to mysql_real_escape your input, $team_name in your case.
[insert obligatory mysql is deprecated warning here ;)]
You can do it by PHP. You don't need a query: mysql_insert_id()
Related
This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 5 years ago.
Maybe a silly question, but I'm really struggling.
I've created a MySQL Database and Table and am trying to push data into it. The data in question is a unique ID and a load of Javascript.
Code is simple:
$sql = "INSERT INTO TableName (id, code)
VALUES ('$uniqueid', '$codeoutput')";
However, whilst I can get the ID in there, I can't get the DB to accept the Javascript (which is currently stored in the variable $codeoutput.
I get the error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
I'm not that familiar with using MySQL, and I have played around with different data types in PhpMyAdmin, but sadly still no luck.
What am I doing wrong? Do I need to have a very specific setup on the column where I'm storing the code? I'm currently trying to store it as medium text.
Is it something to do with needing to escape special characters? Is there an easy solution to this?
simplest way to do it is
$sql = "INSERT INTO TableName ($uniqueid) VALUES ('id')";
$sql2 = "INSERT INTO TableName ($codeoutput) VALUES ('code')";
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I need to update mysql table, and get the number of tables updated using php, the code look like below,
$query = "update ACCESSUSERS set ACTIVE='111' where UPPER(USERNAME)=UPPER('firstname') and PINNUMBER='7777'";
mysqli_query($conn, $query);
$numrows = mysql_affected_rows();
printf("Records updated: %d\n", $numrows);
But the issue is the above print 0 always even if changed the value for ACTIVE with different one.
What can be the issue, any help will be appreciated.
Note: I have tested the above query from phpMyadmin and it's working, only problem while executing in a php.
Thanks,
Haris
You are using the mysqli_ API to make your database query.
You are using the obsolete mysql_ API to count the rows.
You can't switch APIs and expect them to interact with each other. Stick to mysqli_.
Use mysqli_affected_rows instead of mysql_affected_rows
You need to use the mysqli version of affected_rows. Which is
$numrows = mysqli_affected_rows($conn);
This question already has an answer here:
MYSQL last_insert_id() and concurrency
(1 answer)
Closed 8 years ago.
I know that mySQL has the query SELECT LAST_INSERT_ID().
This works properly well when it's a small site and not many users uses it. But, when your website has more users making use of it, there could be a problem with this select, because I can insert something at the same time as another one, so that could generate the problems.
I've been reading something from mySQLi Transactions, but I don't know much about it. Is this the only way to get the last id that I have inserted, not from other that can insert content at the same time as me??
Thanks in advance.
It won't be a problem, because last_insert_id() will get you only the last id inserted in the same session. You don't have to worry.
And as a note, it's just SELECT LAST_INSERT_ID();, not ...FROM table, you don't want to execute this function for every row in the table.
Read more about it here.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get Insert id in MSSQL in PHP?
im working in project by php and sql server 2000, how i can get inserted id like mysql_inserted_id function which work with mysql .
im googled in the internet and cant find something...please leave example to how use it
You probably want to use SCOPE_IDENTITY() (which you can return via a SELECT statement) but the operator you use to get the latest ID depends on your implementation (your stored procedure may do more than one insert, for example, in which case ##IDENTITY would return only the latest ID). Check out this explanation:
http://www.sqlteam.com/article/alternatives-to-identity-in-sql-server-2000
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: how to get last inserted ID of a table?
I'm writing a function to add data do a database, but I then want to immediately use that entry in my code.
I know I could query: SELECT id FROM table ORDER BY id DESC
But I wonder if there's any more direct way of doing it.
That mostly depends on the interface you're using to access MySQL.
If you're using PDO (recommended), you'd use PDO::lastInsertId(): http://us.php.net/manual/en/pdo.lastinsertid.php
If you're using MySQL, you'd use mysql_insert_id() http://php.net/manual/en/function.mysql-insert-id.php
If you're using MySQLi, you'd use mysqli_insert_id(): http://us.php.net/manual/en/mysqli.insert-id.php
Just simply call whichever function is appropriate right after your INSERT completes.
It's important to note that you likely do not want to fetch id out of the database using SELECT because if you have multiple writers, you may get the id that some other thread inserted. At best, you'll operate on the wrong data, and at worst, you could have serious security issues and/or corruption.
With PHP you can use mysql_insert_id() function.
select last_insert_id();
Doc
Maybe this could help:
Get the Unique ID for the Last Inserted Row
If you insert a record into a table
that contains an AUTO_INCREMENT
column, you can obtain the value
stored into that column by calling the
mysql_insert_id() function.