If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Stored procedure query with php

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-20-11, 06:33
slo slo is offline
Registered User
 
Join Date: Nov 2011
Posts: 2
Stored procedure query with php

how do you query mysql stored procedure with php and display the results?... And is it faster to always create a stored procedure for all queries and call it with php?... or a stored procedure should only be created for some queries?... please help ...
Reply With Quote
  #2 (permalink)  
Old 11-20-11, 06:38
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
wonder what google would say?
php mysql stored procedure - Google Search
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 11-21-11, 03:52
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
To call a MySQL stored procedure from within PHP you simply run the following code:

$result = mysql_query("CALL storedProcedure()");

In respect of efficiency it is more efficient to have stored procedures. The level of efficiency is dependent on your hardware setup. I know some web hosting companies that separate their database server domain and the web server domain. As a result, all PHP processing connecting to a database routes all requests over their internal network. As such every request from PHP needs to be sent across the network and responses sent back. This can add to the overall processing time depending on the network response time and throughput. For example let's imagine you want to either insert a new employee based on a name (which should be unique) or update. In your PHP code you would execute the following:

SELECT count(*) INTO count FROM emp WHERE name = 'test';
IF count = 0 THEN
INSERT INTO ...
ELSE
UPDATE emp ...
END IF

In this case 2 trips are made to the server the first to determine whether the entry already exists and based on the result either INSERT or UPDATE.

Using stored procedures in this case will definitely speed up the processing time as only one request is made to the database and all processing happens in memory on the database server.

If the web server and the database server both exist on the same machine, the lag between sending a request and responding to the request is far less (almost insignificant).
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #4 (permalink)  
Old 11-21-11, 06:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
nice explanation, ronan

unfortunately your example is not the best

instead of this --
Quote:
Originally Posted by it-iss.com View Post
SELECT count(*) INTO count FROM emp WHERE name = 'test';
IF count = 0 THEN
INSERT INTO ...
ELSE
UPDATE emp ...
END IF
you can use a single INSERT... ON DUPLICATE KEY UPDATE statement

otherwise, your advice regarding avoiding multiple calls to the database is correct

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 11-21-11, 07:41
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Thanks Rudy, I was trying to provide a very simple example and indeed ON DUPLICATE KEY could have satisfied this example.

Also using stored procedures is even more efficient because it does not have to parse the SQL statements inside as these are usually cached and primed for execution.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On