02
Oct
How we can call store procedure in PHP ?
- Category:
- PHP
Posted On : October 2, 2013
| 1 Comment
Store Procedure is used for improve web application and reduse database request traffic on server.following way we can implement Store procedure and call it from PHP.
Create Database table
View Code SQL
Create table customers (id INT PRIMARY KEY AUTO_INCREMENT , cust_id VARCHAR(50) UNIQUE,name VARCHAR(50)); |
Connect MYSQL Database via PHP.Need to create db_connect.php file
View Code PHP
$hostname="hostname"; // MYsql Hostname $db_user="username"; // Mysql Username $db_pwd="password"; // Mysql Password $db_name="databasename"; // Mysql DB name $conn=mysql_connect($hostname,$db_user,$db_pwd) or die('Database connection refused'); $select_db=mysql_select_db($db_name,$conn) or die('Database name invalid or dosen't exist'); /// databasename = your database name |
You can display customers directly by calling SQL statement
View Code PHP
include("db_connect.php"); // Check code below of the post. $sql_q=mysql_query("SELECT cust_id,name FROM customers"); while($row_q=mysql_fetch_array($sql_q)) { echo $row_q['cust_id'].'--'.$row_q['name'].'</br>'; } |
You can create stored procedures that run on your database server.for example Stored Procedure name customerName().
E.g
View Code SQL
DELIMITER // BEGIN CREATE PROCEDURE customerName() SELECT cust_id,name FROM customers; END |
Now create another PHP file in same folder
For example name as show_customers_proc.php
View Code PHP
include("db_connect.php"); $sql_q=mysql_query("CALL customerName()"); while($row_q=mysql_fetch_array($sql_q)) { echo $row_q['cust_id'].'--'.$row_q['name'].''; } |
that’s it
Hope anyone get this easily and can implement it
- Tags:
Posts
I think this is among the most significant info for me. And i am glad reading your article. But want to remark on some general things, the site style is great, the articles is really excellent. Good job, cheers
Joleen