Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2016-09-05
Medium Priority
Last Modified: 2016-09-28
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.
Question by:Pravin Bnakar
LVL 61

Expert Comment

by:Julian Hansen
ID: 41785627
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
LVL 30

Expert Comment

by:Olaf Doschke
ID: 41785642
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.
LVL 111

Accepted Solution

Ray Paseur earned 2000 total points
ID: 41785904
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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

by:Pravin Bnakar
ID: 41785942
@Ray Paseur
 In object-oriented design should I use stored procedures or use normal php queries?
LVL 111

Expert Comment

by:Ray Paseur
ID: 41785955
Either one is OK.  Use whatever you are more comfortable with!
LVL 30

Expert Comment

by:Olaf Doschke
ID: 41786090
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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question