What is pros and cons between write application with stored procedures in php OR with normal php queries.

Hello, All
   I want to know what is pros and cons between write application with stored procedures in php  or with normal php queries.

Is necessary to write each query with stored procedures.
Which is the best way to create large application like any portal (ecom, travel, property etc.).
in which scenario use stored procedures or use normal php queries.
Pravin BnakarAsked:
Who is Participating?
Ray PaseurConnect With a Mentor Commented:
Which is the best way to create large application like any portal (ecom, travel, property etc.).
in which scenario use stored procedures or use normal php queries.
This sort of question makes me think of the fellow at the automobile dealership.  He's trying to buy a vehicle, and he's basing his decision on the differences between a Hitachi back-up camera and a Sony back-up camera.  If he goes with the Hitachi, he will get a truck.  If he goes with the Sony, he will go home with a sports car.  The backup camera is a misplaced point of emphasis that overlooks the larger picture.

Any implementation of a large application would use object-oriented design.  The first principle of OO design is the Single Responsibility Principle (separation of concerns).  This means that the data model would be abstracted into a series of interfaces that accomplish all the data storage and retrieval.  These interfaces would not be concerned with the details of the database or its queries.  You might try a few different concretions that implement the interfaces, but my general expectation would be that the first successful concretion would be adequate to the success of your project.
Julian HansenCommented:
Stored procedure have a number of advantages over direct queries from PHP

1. It separates your logic layer from your data layer. You can change the mechanics of the SP without changing the interface - so your code remains unchanged

2. It is less prone to injection attacks and other security issues

3. You can control access to the stored procedure with DB security independently of the application using it.

5. A stored procedure is the domain of a DB admin who is skilled at ensuring queries are optimal. Programmers might not have the same skill

6. There can be a performance benefit - stored procedures are cached

7. Network load can be reduced. If your have a complex operation that needs to be done with multiple calls to the DB based on finite set of parameters - you can do that by calling the SP with the parameters in one call rather than multiple round trips

8. Stored procedures can provide the same functionality across multiple applications.

1. Programmability is limited compared to a language like PHP

2. It might require employing an additional (DB) resource so has an impact on cost

3. Only means of grouping functionality is by naming convention
Olaf DoschkeSoftware DeveloperCommented:
You may think stored procedures will perform better, you may think defining stored procedures in the database layer is a better architecturial design than defining the queries in the application business logic layer.

Here's a "rant" on the first assumption: http://www.joinfu.com/2010/05/mysql-stored-procedures-aint-all-that/

Think about other metrics than performance, about maintainability, releaseability, tastabililty, flexibility and portability.

Then look at some CMS or other PHP systems and see what part of the data access logic is put into their MySQL database.

If you look at it from the perspecitive of using a Framework like laravel, which uses Eloquent ORM which is based on ActiveRecord, you are really far off using SQL in the implementation of an application. For a simple create, reade, update, delete example, look here:

You are not concerned about how data access is done, this is managed by frameworks. Notice especially in the create sample code, first an object of a user is created in memory, not in the database. This new user is stored to the database with the save call. It's naturally also with SQL you first need to know what to save before you can do an insert. The intersting part is whether the ActiveRecord class uses stored procs to find,save,update, or delete data. What's your guess?

Bye, Olaf.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Pravin BnakarAuthor Commented:
@Ray Paseur
 In object-oriented design should I use stored procedures or use normal php queries?
Ray PaseurCommented:
Either one is OK.  Use whatever you are more comfortable with!
Olaf DoschkeSoftware DeveloperCommented:
While all Ray says is true there still are implementation decisions having been made by todays frameworks and/or CRMs, some may be old fashioned ways some modern API/CRUD or ORM type of approaches. To be very concrete about stored procs: I haven't seen anything using them, really. Laravel/Eloquent ORM/ActiveRecord in the end use PDO with prepared statements. That's close to stored procs, as it also makes use of caching, and - as the article about the misconception of stored procedure caching says - that's the same amount of caching per session/connection you can only have with stored procs, too, anyway. Besides MySQL is only one possible database to use as backend. ActiveRecord also supports MSSQL and Oracle. And thats thanks to PDO drivers for these databases, but also thanks to declaring that OOP abstraction Ray talks about.

Setting up stored procs you just have another level of things to maintain, need a developer capable to maintain it, and that is not a php developer capable to do MySQL queries alone, you have to have a bit more knowlegde of MySQL specifics in terms of procedures, so this at least has the need for some more experienced PHP/MySQL dev.

You are very new here and I don't know about your experience in PHP or development in general, databases or desktop application/website development, etc. If you begin, I'd not suggest learning MySQL up to stored procs, you'll find more info, tutorials and tools for the approach to handle querying with the "normal queries" approach, of which there are still plenty different ones, obviously starting on mysqli vs PDO and very directly using SQL queries or ORM (object relational mapping) frameworks,

Bye, Olaf.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.