61

I want to be able create stored procedures using phpMyAdmin and later on use it through php.

But I dont know how to?

From what I know, I found out that we cannot manage stored procedures through phpMyAdmin.

What other tool can manage stored procedure?

I am not even sure if it is better option to use stored procedure through PHP. Any suggestion?

Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261

10 Answers10

94

Since a stored procedure is created, altered and dropped using queries you actually CAN manage them using phpMyAdmin.

To create a stored procedure, you can use the following (change as necessary) :

CREATE PROCEDURE sp_test()
BEGIN
  SELECT 'Number of records: ', count(*) from test;
END//

And make sure you set the "Delimiter" field on the SQL tab to //.

Once you created the stored procedure it will appear in the Routines fieldset below your tables (in the Structure tab), and you can easily change/drop it.

To use the stored procedure from PHP you have to execute a CALL query, just like you would do in plain SQL.

wimvds
  • 12,790
  • 2
  • 41
  • 42
  • I can't believe I missed this... It works.. So Well.... Now can you guide me, a place to start learning stored procedures also... – Starx Nov 15 '10 at 10:08
  • I use PHP 2.8.2.4 - There is no the "Delimiter" field anywhere !! ?? – Vassilis Apr 21 '11 at 21:32
  • 1
    @VassilisGr: Then you might consider upgrading phpMyAdmin, it's certainly there in 2.11.8.1 ... – wimvds Apr 28 '11 at 11:17
  • when I call my stored procedure from phpmyadmin it does not return anything, when I execute it there in routines it works and also works when I call it from java, php and others. – Muhammad Mar 20 '15 at 07:19
  • Doesn't work on 3.4.10.1deb over "SQL" tab wirting directly to the textbox. It is mandatory put tags "delimiters ;;" and ";;" at begin/end (;; can be // also or anything I think) – Diego Andrés Díaz Espinoza Jul 14 '15 at 16:50
  • Doesn't work for me on 2.11.11.3 even though the Delimiter field is there on the SQL tab. – BadHorsie Sep 01 '15 at 14:26
60

I guess no one mentioned this so I will write it here. In phpMyAdmin 4.x, there is "Add Routine" link under "Routines" tab at the top row. This link opens a popup dialog where you can write your Stored procedure without worrying about delimiter or template.

enter image description here

Add Routine

enter image description here

Note that for simple test stored procedure, you may want to drop the default parameter which is already given or you can simply set it with a value.

Community
  • 1
  • 1
TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
  • 3
    In order to see that tab you must enable mysqli in the phpMyAdmin's config file. – wmac Apr 10 '14 at 06:55
  • I'd like to add that [this StackOverflow answer](https://stackoverflow.com/a/25988832/6761698) describes how to use a routine within phpmyadmin – Wouter Vanherck Jan 11 '19 at 08:50
25

try this

delimiter ;;

drop procedure if exists test2;;

create procedure test2()

begin

select ‘Hello World’;

end

;;
kapa
  • 77,694
  • 21
  • 158
  • 175
Sourav
  • 259
  • 3
  • 2
1

I got it to work in phpAdmin , but only when I removed the "Number of records " phrase.

In my version of phpAdmin I could see the box for changing the delimiters.

Also to see the procedure in the database i went to the phpAdmin home, then information_schema database and then the routines table.

Reto
  • 11
  • 1
1

The new version of phpMyAdmin (3.5.1) has much better support for stored procedures; including: editing, execution, exporting, PHP code creation, and some debugging.

Make sure you are using the mysqli extension in config.inc.php

$cfg['Servers'][$i]['extension'] = 'mysqli';

Open any database, you'll see a new tab at the top called Routines, then select Add Routine.

The first test I did produced the following error message:

MySQL said: #1558 - Column count of mysql.proc is wrong. Expected 20, found 16. Created with MySQL 50141, now running 50163. Please use mysql_upgrade to fix this error.

Ciuly's Blog provides a good solution, assuming you have command line access. Not sure how you would fix it if you don't.

Paul J
  • 1,489
  • 1
  • 17
  • 19
1
  1. delimiter ;;
  2. CREATE PROCEDURE sp_number_example_records()
  3. BEGIN
    SELECT count(id) from customer; END
  4. ;;
Jay Thakkar
  • 699
  • 1
  • 8
  • 13
1

Try Toad for MySQL - its free and its great.

ronaldosantana
  • 5,112
  • 4
  • 22
  • 28
0

All the answers above are making certain assumptions. There are basic problems in 1and1 and certain other hosting providers are using phpMyAdmin versions which are very old and have an issue that defined delimiter is ; and there is no way to change it from phpMyAdmin. Following are the ways

  1. Create a new PHPMyAdmin on the hosting provider. Attached link gives you idea http://internetbandaid.com/2009/04/05/install-phpmyadmin-on-1and1/
  2. Go thru the complicated route of be able to access the your hosting providers mySQL from your dekstop. It is complicated but the best if you are a serious developer. here is a link to do it http://franklinstrube.com/blog/remote-mysql-administration-for-1and1/

Hope this helps

Sandeep Taneja
  • 169
  • 1
  • 5
0

On local server your following query will work

DELIMITER |

CREATE PROCEDURE sample_sp_with_params (IN empId INT UNSIGNED, OUT oldName VARCHAR(20), INOUT newName VARCHAR(20))

BEGIN

SELECT `first name` into oldName FROM emp where id = empId;

UPDATE emp SET `first name`= newName where id = empId;

END

|

DELIMITER ;

but on production server it might not work. depend on mysql version you are using. I had a same problem on powweb server, i removed delimiter and begin keywords, it works fine. have a look at following query

CREATE PROCEDURE adminsections( IN adminId INT UNSIGNED ) SELECT tbl_adminusersection.ads_name, tbl_adminusersection.ads_controller FROM tbl_adminusersectionright LEFT JOIN tbl_adminusersection ON ( tbl_adminusersectionright.adsr_ads_id = tbl_adminusersection.ads_id ) LEFT JOIN tbl_adminusers ON ( tbl_adminusersectionright.adsr_adusr_id = tbl_adminusers.admusr_id ) WHERE tbl_adminusers.admusr_id = adminId;
Dharman
  • 30,962
  • 25
  • 85
  • 135
Delickate
  • 1,102
  • 1
  • 11
  • 17
0
/*what is wrong with the following?*/
DELIMITER $$

CREATE PROCEDURE GetStatusDescr(
    in  pStatus char(1), 
    out pStatusDescr  char(10))
BEGIN 
    IF (pStatus == 'Y THEN
 SET pStatusDescr = 'Active';
    ELSEIF (pStatus == 'N') THEN
        SET pStatusDescr = 'In-Active';
    ELSE
        SET pStatusDescr = 'Unknown';
    END IF;

END$$
Ajnabi
  • 23
  • 4