I need to call a first procedure (CrushProps) and then loop through each row by calling a second procedure (CrushOutcomes) with a value (PropId) from the row in the results from the first (and return all rows from the new procedure).
I have looked at this question but cannot make it work for me: calling nested stored procedure from php
These are the two stored procedures:
DROP PROCEDURE IF EXISTS CrushProps $$
create procedure CrushProps(IN crID int)
begin
select props.PropText, props.Prop_Id
from props inner join crush_props on props.Prop_Id = crush_props.PropId
where crush_props.CrushId = crID;
end $$
DROP PROCEDURE IF EXISTS CrushOutcomes $$
create procedure CrushOutcomes(IN prID int)
begin
select prop_outcomes.OutcomeText, prop_outcomes.OutcomePoints
from prop_outcomes inner join crush_props ON prop_outcomes.PropId = crush_props.PropId
WHERE prop_outcomes.PropId = prID;
end $$
Being new to PHP and MySQL I have tried many different variations without any luck. The code below runs thru the two procedures sequentially and returns all rows from the first but only one row from the second (and AFTER the full result - not after each row).
I cannot figure out how to make it loop thru each row of the first procedure.
$result = mysqli_query($con,"CALL CrushProps('$crushid')") or die("Query fail: " . mysqli_error());
if($result->num_rows > 0)
{
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$prptxt = $row[PropText];
$prpid = $row[Prop_Id];
echo $prptxt . "<br/>";
}
$result->close();
$con->next_result();
}
$result2 = mysqli_query($con,"CALL CrushOutcomes('$prpid')") or die("Query fail: " . mysqli_error());
if($result2->num_rows > 0)
{
while($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC))
{
$outtxt = $row2[OutcomeText];
$outpts = $row[OutcomePoints];
echo $outtxt . "<br/>";
}
}