how to show procedure output in php - php

i am a php beginer. i want to call a mysql procedure and display the output rows returned by the procedure. the code is below
mysql
delimiter //
create procedure get_ques_list(in p_ques_ctgr varchar(10))
begin
select ques from question_list where ques_categorey=p_ques_ctgr
order by ques;
end //
delimiter ;
php code
$result=mysql_query("CALL GET_QUES_LIST('S_001')");
while($row=mysql_result($result,0,0))
{
echo $row['ques'];
}
but it is giving warning messgge
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in
but the same code works fine and display 5 rows if i use query instead of calling procedure
$result=mysql_query("select ques from question_list where
ques_categorey='S_001' order by ques");
procedure is also working fine when executed in mysql
can anyone tell where m going wrong in that??
any help is appreciated

Related

sql query not working for stored procedure in PHP

I have a stored procedure in sql server.When I try to use the php function to get the results,its not working.
echo mssql_query(exec MOB_uspcommunication_details $userID,'send');
This gives result as 1 instead of resourceID.When I run the query in sql server its giving me the result.
When I change the second parameter to some other value like this,its working from PHP side.
echo mssql_query(exec MOB_uspcommunication_details $userID,'inbox');
This gives me resourceid#10 and I am getting the results.
For the first query,I am not able to understand why this happens.Please help me
From the PHP documentation:
Return Values
Returns a MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error.
So that means that the first function call returns 1 to indicate there were no rows (1 == true in PHP). The second function call actually retrieves the rows from your table.

MySQL Stored Procedure select not returning anything

I have the following stored procedure in a MySQL db. It doesn't return anything when I run it from my PHP code(Which I know is correct because it works for all of my other stored procedures). When I log into phpmyadmin and execute the stored procedure from the "Routines" page it works just fine. Any help would be much appreciated.
DELIMITER //
drop procedure if exists spGetTeam //
CREATE PROCEDURE spGetTeam(IN tid INT)
BEGIN
SELECT team_id, team_name FROM teams WHERE team_id = tid;
END //
DELIMITER ;
I'm calling the sp from my php like so:
$sql = 'CALL spGetTeam(2)';
The sp is called with the following:
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_assoc($result)) {
//handle return
}
This started working the next day. Not sure what happened. No code was changed. If I ever figure it out I will update the answer.

jTable jQuery plugin why is my MySQL stored procedure failing?

I am trying to implement a filter in jTable. Here is the jTable Filtering Demo.
I used the jQuery code supplied by the demo to sucessfully send POST data to the listAction php script. I access this data and assign it to the $filter variable. I also add two wildcards before and after the string to tell MySQL to perform a comprehensive search rather then an index search.
$filter = '%';
$filter .= $_POST['name'];
$filter .= '%';
$startIndex = $_GET['jtStartIndex'];
$pageSize = $_GET['jtPageSize'];
$sorting = $_GET['jtSorting'];
I then call the stored procedure and pass in each variable. I removed actual database and procedure name, but I verified they are correct.
$mysqli->query("USE data;");
$res = $mysqli->query("CALL proced($startIndex, $pageSize, $filter);");
$res returns false here.
Calling Stored Procedures DEFINITELY works as I have other functionalities working this way. I have other Stored Procedures working that simply return 0 to 9 records correctly. Here is the current Stored Procedure code:
CREATE DEFINER=`root`#`localhost` PROCEDURE `proced`(startIndex INTEGER, pageSize INTEGER, filter VARCHAR(255))
BEGIN
SELECT * FROM reports
WHERE `new`=0 AND ReportingName LIKE filter
ORDER BY idReports ASC
LIMIT startIndex, pageSize;
END
Passing in values in MySQL workbench works and the query returns the correct rows:
call proced(
0,
10,
'Art%'
);
But jTable fails with "An error occured while communicating to the server."
Can anyone point me in the right direction?
Got this working by getting an error back from MySQL:
if(!$res){
ChromePhp::log($mysqli->error);
die('error ['. $mysqli->error . ']');
}
Error:
Unknown column 'Art' in 'field list'
Solution(copied from link):
Try using different quotes for "Art" as the identifier quote character is the backtick (“`”). Otherwise MySQL "thinks" that you point to a column named "Art".
See also MySQL 5 Documentation
So simply by adding single quotes to the passed in String parameter works:
CALL procedure('$filter');

Passing variables from SQL proceedure to PHP

I am trying to create an sql proceedure that will return the results back to the php page.
I want to be able to call the procedure as follows from the php
call procedure_name($var1)
which will run this script:
-- ---------------------------------------------------------------------------------
-- pUIGetCliStmtGenFlag
--
-- This procedure returns the status of the Trading Period:
--
-- ---------------------------------------------------------------------------------
drop procedure if exists pUIGetCliStmtGenFlag;
delimiter //
create procedure pUIGetCliStmtGenFlag(
IN pTradingPeriodMonth DATE
)
MODIFIES SQL DATA
COMMENT 'Checks if the TP has been closed'
begin
SELECT trading_period_month,
dt_end,
amt_traded_system_ccy
FROM ca_trading_period
WHERE trading_period_month=$var1
-- If amt_traded_system_ccy is NULL give the TP an open status otherwise mark as closed
IF amt_traded_system_ccy is NULL
$tpstatus='open'
ELSE
$tpstatus='closed'
end;
//
delimiter ;
I then want to be able to use $tpstatus in the rest of the php script.
I know this is simple but this is completely new to me and I cant find the correct method
You have to call the stored procedure from php and THEN get the values back, using OUT parameters of the procedure.
Please follow these instructions:
https://stackoverflow.com/a/48161/1291428

php mysqli to call stored proc, get get results

I have a stored proc that does a geospatial query. The proc issues two sql statements but only the 2nd one does a query but unfortunately both statements produce a result set. I need the second result set which contains the results of the actual query.
The first statement sets a bounding box:
SET #bbox = 'POLYGON(($polygon))'; \n
SELECT * , AsText( location )
FROM users
WHERE Intersects( location, GeomFromText( #bbox ) ) [snipped for brevity]
If I run the above in phpMyAdmin, it works but I get the following message AFTER the SET command is issued and I want to throw this away:
# MySQL returned an empty result set (i.e. zero rows).
On the php side, I build the query string, calling the stored proc and on return the first thing I do is throw away the empty result set.
$query = "CALL usp_queryByPolygon('$polygon', $msg_id, $user_type)";
$result = mysqli_query($cxn, $query) or die("GEOCODE: queryPolygon - " .sql_error());
sql_free_result($result);
After throwing away the result set I now need the results of the query and this is what I have done:
$result = sql_next_result();
The problem is when I try to use this second result set as in:
if(mysqli_num_rows($result) > 0)
I get errors:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given
in /blah/blah/module.php on line 96
To complicate things, all of the above is in a loop and there could be dozens or 100's of polygons to search.
So the question is this: what is the proper way to get that 2nd result set?
You'd better be accurate of what functions you execute. sql_next_result() is no standard PHP function, nor is it in MySQLi which you seem to use. If it's some kind of database class, please just show the methods that class uses. Nobody here can but quess what sql_next_result() does.
Assuming you're talking about mysqli_next_result(), that indeed returns a boolean, you need to call mysqli_use_result() after that in order to retreive the next result set.
Found out the two statements: SET #bbox and SELECT can be executed sequentially so mysqli and the two results are just fodder that don't need to be dealt with.

Categories