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
59 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 51

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 29

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 108

Accepted Solution

by:
Ray Paseur earned 500 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 108

Expert Comment

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

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

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Button Click 11 26
php image upload 3 27
regex expression 9 23
Datepicker in PHP 9 21
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now