gpt4 book ai didi

php - 调用mysql存储过程后执行查询

转载 作者:行者123 更新时间:2023-11-29 06:52:05 26 4
gpt4 key购买 nike

我对 mysql 中的存储过程有一些疑问。这是我的存储过程代码:

DELIMITER $$

CREATE PROCEDURE `projects_grid`(in pagination varchar(100))
BEGIN
DECLARE a,b INT;
declare percent float;
DECLARE cur_1 CURSOR FOR SELECT id FROM tbl_projects;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET b = 1;
drop temporary table IF EXISTS tbl_temp ;
create Temporary table tbl_temp(id int,name varchar(100) charset utf8,project_name varchar(100) charset utf8,project_type varchar(100) charset utf8,start_date int,end_date int,percent varchar(5));
OPEN cur_1;
REPEAT
FETCH cur_1 INTO a;
set @name='';
set @project_type='';
set @project_name='';
set @b_date='';
set @e_date='';
set @percent='';
set @e_id='';
set @p_id='';
set @completed_rows_count=0;
set @rows_count=0;
select project, (select name from tbl_client where id=tbl_projects.employer_id) as name,(select name from tbl_project_type where id=tbl_projects.project_type_id) as project_name,contract_begin_date,contract_terminate_date into @project_name,@name,@project_type,@b_date,@e_date from tbl_projects where id=a;
SELECT count(PS.status) into @completed_rows_count FROM tbl_projects_status AS PS JOIN tbl_projects_results AS S ON PS.projects_results_id = S.id where project_id=a and PS.status='1';
SELECT count(PS.status) into @rows_count FROM tbl_projects_status AS PS JOIN tbl_projects_results AS S ON PS.projects_results_id = S.id where project_id=a;
set percent=(@completed_rows_count/@rows_count)*100;
insert into tbl_temp values(a,@project_name,@name,@project_type,@b_date,@e_date,concat(percent,'%'));
UNTIL b = 1
END REPEAT;
CLOSE cur_1;
set @pagination=pagination;
set @query=concat('SELECT distinct(id),name,project_name,project_type,start_date,end_date,percent FROM tbl_temp ',@pagination);
PREPARE stmp FROM @query;
EXECUTE stmp;
DEALLOCATE PREPARE stmp;
END

这是我的 php 代码:

<?php
require_once 'bootstrap.php';
$query="call projects_grid('')";
$result=mysql_fetch_array(mysql_query($query));
foreach($result as $record)
{
echo $record;
}
$query="select count(*) from tbl_projects";
$result=mysql_query($query); // Boolean false given :(
$result=mysql_fetch_row($result);
echo $result[0];
?>

当我在程序后执行查询时,在 Debug模式下,系统显示给定的 bool 值为 false!。调用存储过程后如何执行查询?

最佳答案

终于找到答案了:使用 mysqli 代替 mysql。

$mysqli = new mysqli('your_hostname','your_username' ,'your_password','your_db_name');
$result=$mysqli->query($query);
$result->data_seek(0);

执行查询后,您必须使用以下命令:

mysqli_free_result();
mysqli_next_result($mysqli);

祝你好运!

关于php - 调用mysql存储过程后执行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14725025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com