How to use mysql stored procedures on php

I’ll try to make this short because I have already discussed some essentials on mysql stored procedures here.

Requirements:

  • WAMP Server
  • Heidi SQL
  • Notepad++ or any text editor with line numbers.

I’ll only be discussing how to actually call the stored procedure from your php application, so please read this first if you don’t know how to create stored procedures on heidi sql.

So in your notepad++ open up the php file where your query is.

It should look something like:

<?php
include('conn.php');
 
$name=$_POST['neym'];
$course=$_POST['corz'];
 
$sql="INSERT INTO students(name, course) VALUES('$name', '$course')";
$res=mysql_query($sql);
 
?>

Where the include part is where I inherit from the conn.php file for the mysql connection essentials. The conn.php file could look something like this:

<?php
$host="localhost";
$uname="root";
$pword="secret";
$con=mysql_connect('$host', '$uname', '$pword' );
$sdb=mysql_select_db($con, 'databaseName');
?>

mysql_connect and mysql_select_db are built in methods in php to connect to mysql. Often times the host name is ‘localhost’ and the username is ‘root’. There is no password by default. But I just inputted here ‘secret’ as the password.

Now, if you want to use the stored procedure that you have created. Then just call it from your php file. Just replace the actual sql query with the method call:

$sql="call stored_procedure_name('$name', '$course')";

That’s just an example, the parameters could expand depending on the fields that are on your table. Don’t forget to replace ‘stored_procedure_name’ with the actual name of the stored procedure that you have created in heidi sql.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s