我在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];
?>
当我在程序之后执行查询时,在调试模式下,系统显示给定的boolean为false!。 如何在调用存储过程后执行查询?
答案 0 :(得分:2)
最后我找到了答案: 使用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);
祝你好运!
答案 1 :(得分:0)
我们可以在manual page中找到mysql_query()
函数的用法示例:
$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
die('Invalid query: ' . mysql_error());
}
总结一下:
mysql_query()
在查询失败时返回false
mysql_error()
返回错误消息<子> P.S。您还可以看到“此扩展程序自PHP 5.5.0起已弃用,将来会被删除”警告但我想您已经知道了。
编辑: “命令不同步;您现在无法运行此命令”错误消息通常可以通过释放前一个结果集{{{ 3}}。这并不是说你的存储过程中存在一些错误 - 遗留的mysql扩展并没有真正考虑到存储过程。