Help! PHP & PostgreSQL/SQLite/MSSQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MisterB
Forum Newbie
Posts: 5
Joined: Fri Sep 23, 2011 2:41 pm

Help! PHP & PostgreSQL/SQLite/MSSQL

Post by MisterB »

Hello everyone,

I used to develop all my projects using MySQL databases... But now I want to change a bit and offer more than one choice... So I was looking for equivalent functions for mysql_insert_id() function, but it seems that not all the database engines handle that, that's what I found:

PostgreSQL equivalent: pg_last_oid($row)
$row is apparently the variable of the last query inserted in database, unlike MySQL, PgSQL needs to know which query was last ran?
Also, they mentioned that the OID (return value) is a STRING not and INTEGER, what is exactly OID so?

SQLite equivalent: sqlite_last_insert_rowid($dbhandler)
what is $dbhandler?

MSSQL equivalent would be the following function:
function mssql_lastid()
{
$id = 0;
$res = mssql_query("SELECT @@identity AS id");
if ($row = mssql_fetch_row($res)) $id = $row[0];
return $id;
}

Is that right?
:) Thanks
nowaydown1
Forum Contributor
Posts: 169
Joined: Sun Apr 27, 2008 1:22 am

Re: Help! PHP & PostgreSQL/SQLite/MSSQL

Post by nowaydown1 »

Welcome to the forum. Not to discourage you from pursuing this project out of personal interest, but if your end goal is to simply be compatible across relational database engines, you may be better served by looking at some of the packages that exist that provide this functionality for you out of the box. One package that comes to mind is MDB2 (http://pear.php.net/package/MDB2). I'm sure there are other packages out there just as good.

As you rightly pointed out, different relational databases will have different ways of getting things like the insert id. A database abstraction layer will take care of these differences for you, and provide you with a common interface that allows you to focus on your project and not worry as much about the underlying storage engine. There are still some concerns depending on how you develop your app. Not all SQL is created equal as you'll find out :)
MisterB
Forum Newbie
Posts: 5
Joined: Fri Sep 23, 2011 2:41 pm

Re: Help! PHP & PostgreSQL/SQLite/MSSQL

Post by MisterB »

Thank you for your answer nowaydown1 :) I didn't know that there're some interfaces that can help me to manage that... I was about to re-code each SQL function :o
Post Reply