php


PHP Get the last insert id from ODBC connection


How do I get the last insert id from a database using a ODBC connection?
I'm looking for a solution similar to the mysql_insert_id() function.
If you're using databases with PHP I strongly recommend using PDO (simple database wrapper for a lot of common database engines, more and more supported all the time, part of PHP canon), and hence use PDO::lastInsertId if your database supports the equivalent of mysql_insert_id.
Don't use "SELECT max(id) FROM table;" as it can result in seriously freaky and hard-to-find bugs later on.
* **UPDATE : Ok, you're using ODBC, and I suspect you're after odbc_cursor. I still stand by the strong recomendation to use PDO, as it has an ODBC driver. (ODBC in my eyes is an grumpy bitter old man who mumbles under his breath driving his truck that's falling apart, as the hip and effective PDO guys race past in their sexy VOLVO S90's)
SELECT ##IDENTITY AS ID
Use in MySQL
SELECT *
FROM table_name
ORDER BY Id Desc
LIMIT 1
Use in SQL Server
SELECT top 1 *
FROM table_name
ORDER BY Id Desc
It depends on the database type, but look into the SEQUENCE syntax for your rdbm.
Are you inserting rows into your database via PHP? If so, perhaps you can generate a unique primary key using uniqid() - then you will know the ID without having to query the database.
If it is not possible to update the key type, perhaps you can still insert a unique id when you do the inserts, so you could do a query like this:
SELECT id FROM mytable WHERE rowid = '$myuniqueid'
That way you're ensuring that you're pulling back the correct ID from the database - a much better solution than MAX(id).
I used "SELECT ##IDENTITY AS LastID", while working with PHP/MSSQL, through ODBC. That brought some issues under SQL 2005 server (or was it 2000?).
Either way if you do like this:
SET NOCOUNT ON
[NOW INSERT YOUR INSERT SQL QUERY]
SELECT ##IDENTITY AS LastID
you should be just fine in any MSSQL server version.
"SET NOCOUNT ON" will prevent sql server from sending messages like '1 rows inserted', which will keep your script working properly.
If you have MySQL under ODBC - you can use the next query:
"SELECT LAST_INSERT_ID( );"
It have to be executed mmediately after executing INSERT-query.
For other database - use other specific queries...
Using
SELECT max(id) FROM table;
should be fine from inside a transaction, or does ODBC not support transactions?

Related Links

PHP - Using WHERE in INSERT ON DUPLICATE UPDATE issue
Prepared Statement not working “ right syntax to use near ?” [duplicate]
Undefined index error - Defined index after the variable is called
Replace '&' with '&' in PHP string
codeigniter redirects to another page without executing controller
What happens if i disable some module in OctoberCMS?
Imagick Failed to read the file PDF
CakePHP images and JS throwing 404
Getting “bson_decimal128_from_string” error trying to build mongo-php-driver
Regex Repeating Pattern to Capture All HTML Table Column Contents
PHP form not sending to email address [duplicate]
Twig 1.24 don't have dump function
Why doesn`t work call to https Rest API using curl in php?
laravel SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (SQL: insert into) in laravel
UPDATE SQL query won't update database in my php script
Get a strange array value

Categories

HOME
spring-data
macos-sierra
softlayer
antd
mousemove
autocomplete
openflow
share
haxe
bellman-ford
swarm
lapack
game-maker
fullcalendar-scheduler
multi-step
bundler
amazon-elb
distributed-computing
susy-sass
android-custom-view
social-tables
error-logging
altium-designer
conda
websphere-portal
substring
pyopencl
pdf.js
plyr
cellular-network
morris.js
crud
list-comprehension
offline-caching
network-analysis
points
history.js
tsung
basic-authentication
winsock
delayed-job
dreamfactory
bluemixtools
runtimeexception
stackpanel
spring-restcontroller
stat
suds
jacoco-maven-plugin
plane
resampling
hypothesis-test
pyttsx
httphandler
oracle-bmcs
vesta
wysihtml5
sony-future-lab-n
brightscript
sharpdx
visual-format-language
sigsegv
aurelia-fetch-client
palindrome
sesame
haraka
rational-performance-test
google-identity-toolkit
nclam
s-function
nunit-console
tsqlt
dojo-build
gnucash
noclassdeffounderror
seadragon
gdl
openshift-cartridge
mixins
knife
supersocket.net
ngcordova
sequence-sql
kefir.js
vs-unit-testing-framework
device-admin
adwords-apiv201402
siena
windows-messages
neo4jphp
rails-api
miglayout
oxygene
spring-social-linkedin
particle-swarm
django-settings
blobstorage
dibs
asp.net-mvc-3-areas
jsplitpane
xalan
onconfigurationchanged
struts2-json-plugin
live-wallpaper
viewpagerindicator
coordinate
uitextfielddelegate
image-scanner
series-40
servercontrol
vows
rtd
qcar-sdk
pinax
fgetc
powergui
iphone-sdk-3.1.3
error-detection

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App