2

Is there a way of using functions defined in PHP in mysql? Please help.

2
  • 2
    Can you elaborate a bit? Commented May 13, 2011 at 4:20
  • for instance :select * from table where (it_exists(table.name)) it_exists is a php function Commented May 13, 2011 at 17:42

2 Answers 2

3

Yes there is a way to accomplish what you are talking about. They are called "Stored Procedures" and you can create one using PHP and then store it on MySQL Server, this way the execution of the function will my the MySQL server's responsibility instead of the PHP Engine.

Here is a link to the MySQL documentation on Stored Procedures:

Stored Procedures ,

Another article on Stored Procedures ,

Using Stored Procedures ,

I don't know if this answers your question, but I figured I would give it a shot.

EDIT: Check out my comments on Tudor Constantin's answer, you CAN write stored procedures to the database using a PHP MySQL query! The stored procedures get placed in the mysql database in the "proc" table on the mysql server.

h

Sign up to request clarification or add additional context in comments.

Comments

3

No, there isn't. There are some equivalent functions, but SQL (the language used by MySQL) and PHP are 2 different technologies/platforms/softwares/programming paradigms/etc and they have totally different set of instructions/principles of programming.

Instead in PostgreSQL you can write stored procedures in PHP

7 Comments

@Tudor Constantin, ha ha, nice job man, you posted just before me :P and I like how our statements at first seem contradictory, but at second glance are essentially saying the same thing with emphasis in different areas.
@hypervisor666 - yesterday the same thing happened to me - somebody else got to respond 30 seconds earlier the same answer to the same question - however, i don't think that you can wright stoderd procedures in MySQL in other language than SQL
@Tudor Constantin, Hey man, you made me doubt myself and I just tested it and got it to work, I was able to write a MySQL stored procedure using standard issue PHP mysql calls. You do it just like an INSERT statement, but you still write the query string using SQL language and syntax :)
@Tudor Constantin, I should add that I was not certain of myself when I answered this question, I'm glad you called my bluff, it made me look it up to make certain and actually test it in my dev environment. :D thanks! Also just for reference to anyone reading this, the stored procedures I created got dropped into the "mysql" database, inside the "proc" table. I used phpmyadmin to view and verify their existence. The mysql query string was basic and looked like this: $query = "CREATE PROCEDURE p99 () SELECT * FROM $this->tbname";
@hypervisor - yes, you are able to write stored 'in php' and send them to the MySQL server - I was only saying that the instructions that MySQL will execute have to be written in SQL. Whith PostgreSQL you can write stored procedures in a dozen languages, and that code will be executed by the DB engine, not by application/web server
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.