PDA

View Full Version : Object tabs in mysql


Nirbhay
09-04-2009, 06:01 AM
Do we have anything equivalent to object tabs(PLSQL) in mysql.
And also how to call a user defined mysql function in PHP.

Thanx in advance ...

Old Pedant
09-04-2009, 11:11 PM
No idea what an "object tab" is.

And when you say "user defined function", do you mean a stored function, made via standard CREATE FUNCTION syntax, or do you mean functions written in C/C++??

Though as far as calling either kind from a SQL statement goes, I don't know of any particularly special requirements.

SKDevelopment
09-05-2009, 05:47 PM
Maybe object types (http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14261/objects.htm) are meant ? As far as i know MySQL does not have anything like that yet.

As to calling user defined mysql functions, Old Pedant is absolutely right. You simply create a function with CREATE FUNCTION (http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html) statement and then call it from your SQL statement e.g. like

SELECT my_func() from my_table WHERE <some condition>;

which is called from PHP via mysql_query() (http://php.net/mysql_query) as any other query.

I am sorry if I if I have not understood the question. In this case could you reformulate it or give a little bit more information please ?

Nirbhay
09-07-2009, 05:46 AM
Thanx for the response ..

can you please tell how to call the user defined function of Mysql with argument from PHP

SKDevelopment
09-07-2009, 12:00 PM
Yes, of course. First you create a MySQL stored function with a parameter using the CREATE FUNCTION (http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html) syntax.

Then you could call your stored function inside any SQL statement in PHP in the following way:

$query = mysql_query("SELECT my_func('$my_argument') from my_table WHERE <some condition>");

In this example I supposed $my_argument was a string so I surrounded it with single quotes. If it was numeric, I would not use quotes around it.

Of course $my_argument should be properly escaped/validated before using it in the query. You need to take all possible precautions to avoid SQL injections if your arguments come from any potential user input like arrays $_GET, $_POST, $_COOKIE or other sources which could not be trusted. For reference on escaping/validation please see mysql_real_escape_string() (http://php.net/mysql_real_escape_string).