如何使用phpmyadmin编写存储过程以及如何通过php使用它?

发布于 2024-09-01 18:13:20 字数 215 浏览 3 评论 0原文

我希望能够使用 phpMyAdmin 创建存储过程,然后通过 php 使用它。

但我不知道该怎么办?

据我所知,我发现我们无法通过phpMyAdmin来管理存储过程。

还有什么其他工具可以管理存储过程?

我什至不确定通过 PHP 使用存储过程是否是更好的选择。 有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

娇女薄笑 2024-09-08 18:13:20

由于存储过程是使用查询创建、更改和删除的,因此您实际上可以使用 phpMyAdmin 来管理它们。

要创建存储过程,您可以使用以下命令(根据需要进行更改):

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

并确保将 SQL 选项卡上的“分隔符”字段设置为 //。

创建存储过程后,它将出现在表下方的例程字段集中(在“结构”选项卡中),并且您可以轻松更改/删除它。

要使用 PHP 中的存储过程,您必须执行 CALL 查询,就像在普通 SQL 中执行的操作一样。

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.

惜醉颜 2024-09-08 18:13:20

我想没有人提到这一点,所以我就写在这里。在 phpMyAdmin 4.x 中,顶行“例程”选项卡下有“添加例程”链接。此链接将打开一个弹出对话框,您可以在其中编写存储过程,而不必担心分隔符或模板。

在此处输入图像描述

添加例程

在此处输入图像描述

请注意,对于简单的测试存储过程,您可能希望删除已经给出的默认参数,或者您可以简单地为其设置一个值。

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.

痴情换悲伤 2024-09-08 18:13:20

试试这个

delimiter ;;

drop procedure if exists test2;;

create procedure test2()

begin

select ‘Hello World’;

end

;;

try this

delimiter ;;

drop procedure if exists test2;;

create procedure test2()

begin

select ‘Hello World’;

end

;;
盗心人 2024-09-08 18:13:20

尝试 Toad for MySQL - 它免费且非常棒。

Try Toad for MySQL - its free and its great.

情定在深秋 2024-09-08 18:13:20

我让它在 phpAdmin 中工作,但只有当我删除“记录数”短语时。

在我的 phpAdmin 版本中,我可以看到用于更改分隔符的框。

为了查看数据库中的过程,我去了 phpAdmin 主页,然后是 information_schema 数据库,然后是例程表。

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.

夜访吸血鬼 2024-09-08 18:13:20

新版本的 phpMyAdmin (3.5.1) 对存储过程的支持有了更好的表现;包括:编辑、执行、导出、PHP代码创建和一些调试。

确保您在 config.inc.php 中使用 mysqli 扩展

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

打开任意数据库,您将在顶部看到一个名为例程的新选项卡,然后选择添加例程

我所做的第一个测试产生了以下错误消息:

MySQL 说:#1558 - mysql.proc 的列计数错误。预期为 20,发现为 16。使用 MySQL 50141 创建,现在运行 50163。请使用 mysql_upgrade 修复此错误。

Ciuly 的博客< /a> 提供了一个很好的解决方案,假设您有命令行访问权限。如果不这样做,不确定如何解决它。

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.

找个人就嫁了吧 2024-09-08 18:13:20
  1. 分隔符;;
  2. 创建过程 sp_number_example_records()
  3. 开始
    从客户中选择计数(id);
    结尾
  4. ;;
  1. delimiter ;;
  2. CREATE PROCEDURE sp_number_example_records()
  3. BEGIN
    SELECT count(id) from customer;
    END
  4. ;;
陌上青苔 2024-09-08 18:13:20

以上所有答案都是做出一定的假设。 1and1 中存在基本问题,某些其他托管提供商正在使用非常旧的 phpMyAdmin 版本,并且存在定义分隔符的问题;并且无法从 phpMyAdmin 更改它。 方法

  1. 以下是在托管提供商上创建新 PHPMyAdmin 的 。附加链接为您提供想法 http://internetbandaid.com/2009/04 /05/install-phpmyadmin-on-1and1/
  2. 通过复杂的途径能够从您的桌面访问您的托管提供商 mySQL。它很复杂,但如果您是认真的开发人员,这是最好的。这是一个链接http://franklinstrube.com/blog/remote -mysql-administration-for-1and1/

希望这有帮助

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

夜清冷一曲。 2024-09-08 18:13:20
/*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$
/*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$
黑寡妇 2024-09-08 18:13:20

在本地服务器上,您的以下查询将起作用,

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 ;

但在生产服务器上可能不起作用。取决于您使用的 mysql 版本。我在 powweb 服务器上遇到了同样的问题,我删除了分隔符和开始关键字,它工作正常。
看看下面的查询

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文