PHP变量传人MySQL,如何从PHP将变量传递到MySQL存储过程

本文介绍了一个MySQL存储过程,用于构建代理层级结构并按佣金级别降序排列。该过程使用临时表来递归地填充代理及其上级代理的关系,并讨论了如何通过输入参数动态改变排序字段的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have the following stored procedure:

proc_main:begin

declare done tinyint unsigned default 0;

declare dpth smallint unsigned default 0;

create temporary table hier(

AGTREFERRER int unsigned,

AGTNO int unsigned,

depth smallint unsigned default 0

)engine = memory;

insert into hier values (p_agent_id, p_agent_id, dpth);

/* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */

create temporary table tmp engine=memory select * from hier;

while done <> 1 do

if exists( select 1 from agents a inner join hier on a.AGTREFERRER = hier.AGTNO and hier.depth = dpth) then

insert into hier

select a.AGTREFERRER, a.AGTNO, dpth + 1 from agents a

inner join tmp on a.AGTREFERRER = tmp.AGTNO and tmp.depth = dpth;

set dpth = dpth + 1;

truncate table tmp;

insert into tmp select * from hier where depth = dpth;

else

set done = 1;

end if;

end while;

select

a.AGTNO,

a.AGTLNAME as agent_name,

if(a.AGTNO = b.AGTNO, null, b.AGTNO) as AGTREFERRER,

if(a.AGTNO = b.AGTNO, null, b.AGTLNAME) as parent_agent_name,

hier.depth,

a.AGTCOMMLVL

from

hier

inner join agents a on hier.AGTNO = a.AGTNO

inner join agents b on hier.AGTREFERRER = b.AGTNO

order by

-- dont want to sort by depth but by commission instead - i think ??

-- hier.depth, hier.agent_id;

a.AGTCOMMLVL desc;

drop temporary table if exists hier;

drop temporary table if exists tmp;

end proc_main

While the function does its job well - it only currently allows sorting via AGTCOMMLVL descending order. The stored procedure's purpose is to match a memberID with their parentID and associated COMMLVL. Once paired appropriately,I use the memberID in a second query to return information about that particular member.

I would like to be able to sort by any number of filters but have the following problems:

I can't seem to find a way to pass a variable into the stored procedure altering its sorting by field.

Even if I could - the sort may actually only contain data from the second query (such as first name, last name, etc)

Running a sort in the second query does nothing even though syntax is correct - it always falls back to the stored procedure's sort.

any ideas?

EDIT

My php uses mysqli with code:

$sql = sprintf("call agent_hier2(%d)", $agtid);

$resulta = $mysqli->query($sql, MYSQLI_STORE_RESULT) or exit(mysqli_error($mysqli));

解决方案

If you want to sort by input parameter of the stored procedure, you need to use Prepared staments

For example,

DELIMITER //

CREATE PROCEDURE `test1`(IN field_name VARCHAR(40) )

BEGIN

SET @qr = CONCAT ("SELECT * FROM table_name ORDER BY ", field_name);

PREPARE stmt FROM @qr;

EXECUTE stmt;

DEALLOCATE PREPARE stmt;

END //

资源下载链接为: https://pan.quark.cn/s/9e7ef05254f8 在网页设计中,为图片添加文字是一种常见的需求,用于增强视觉效果或达更多信息。本文将介绍两种常用的方法:一种是将图片设置为背景并添加文字;另一种是利用<span>标签结合CSS定位来实现。 这种方法通过CSS实现,将图片设置为一个容器(通常是<div>)的背景,然后在容器中添加文字。具体步骤如下: 创建一个包含文字的<div>元素: 使用CSS设置<div>的背景图片,并调整其尺寸以匹配图片大小: 如有需要,可使用background-position属性调整图片位置,确保文字显示在合适位置。这样,文字就会显示在图片之上。 另一种方法是将文字放在<span>标签内,并通过CSS绝对定位将其放置在图片上。步骤如下: 创建一个包含图片和<span>标签的<div>: 设置<div>为相对定位,以便内部元素可以相对于它进行绝对定位: 设置<span>为绝对定位,并通过调整top和left属性来确定文字在图片上的位置: 这种方法的优点是可以精确控制文字的位置,并且可以灵活调整文字的样式,如颜色和字体大小。 两种方法各有优势,可根据实际需求选择。在实际开发中,还可以结合JavaScript或jQuery动态添加文字,实现更复杂的交互效果。通过合理运用HTML和CSS,我们可以在图片上添加文字,创造出更具吸引力的视觉效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值