Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-05
6
Medium Priority
?
145 Views
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.
0
Comment
Question by:Pravin Bnakar
6 Comments
 
LVL 60

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.

Drawbacks
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
1
 
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:
http://www.phpactiverecord.org/projects/3/wiki/Basic_CRUD

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.
1
 
LVL 111

Accepted Solution

by:
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.
1
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Expert Comment

by:Ray Paseur
ID: 41785955
Either one is OK.  Use whatever you are more comfortable with!
0
 
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.
1

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

972 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