2013-02-06 44 views
1

我在存儲過程中有一些問題在mysql中。 這是我的存儲過程的代碼:執行查詢後調用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爲假的! 。 如何在調用存儲過程之後執行查詢?

+0

這是因爲你沒有權限在該表上激發'select'。請參閱http://php.net/manual/en/function.mysql-query.php – asifsid88

回答

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); 

祝你好運!

0

我們可以找到的mysql_query()功能的使用例子在其manual page

$result = mysql_query('SELECT * WHERE 1=1'); 
if (!$result) { 
    die('Invalid query: ' . mysql_error()); 
} 

綜上所述:

  1. mysql_query()回報false當查詢失敗
  2. mysql_error()返回錯誤信息

P.S.您也可以看到「此擴展從PHP 5.5.0開始已被棄用,並且將來會被移除」警告,但我想您已經知道了。

編輯:「命令不同步,你無法運行此命令現在」錯誤信息通常可以通過釋放先前的結果與mysql_free_result()設置固定。這並不是說你的存儲過程中肯定有什麼錯誤 - 遺留的mysql擴展並不是真正用存儲例程設計的。

+0

我的查詢工作正常時,我不使用存儲過程。它也適用於phpmyAdmin。 –

+0

在這種情況下,您不需要閱讀錯誤消息。猜測是更有趣! –

+0

錯誤:無效的查詢:命令不同步;你現在不能運行這個命令 –