Page 1 of 1

Help! PHP & PostgreSQL/SQLite/MSSQL

Posted: Fri Sep 23, 2011 3:07 pm
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

Re: Help! PHP & PostgreSQL/SQLite/MSSQL

Posted: Sat Sep 24, 2011 12:37 am
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 :)

Re: Help! PHP & PostgreSQL/SQLite/MSSQL

Posted: Sat Sep 24, 2011 4:25 am
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