I am new to stored procedures and am using mysql. I am creating the procedures in phpmyadmin.
What I need to achieve is a stored procedure which will return all user details from one table and their associated relations in a linking table.
I need my php web service to call the procedure and be able to return a json similar to the following:
{
"userID": "2222",
"userName": "mr x",
"userAddress": "street, town, County",
"userMobile": "1111",
"userPhone2": "2222",
"latitude": "53.2802937",
"longitude": "-9.0515995",
"relations": [
{
"relID": "3333",
"relFName": "Mr",
"relSName": "rel1",
"relAddress": "street2, town2, county2",
"relPhone": "3333",
"relPhone2": "3333",
"relType": "Brother"
},
{
"relID": "4444",
"relFName": "ms",
"relSName": "rel4",
"relAddress": "street 4, town 4, county 4",
"relPhone": "4444",
"relPhone2": "4444",
"relType": "Friend"
}
]
}
I currently have two stored procedures i.e.
getUsers:
BEGIN
SELECT * FROM Users ORDER BY userSName DESC;
END
and getRels:
BEGIN
SELECT * FROM relations WHERE relations.userID = UserIDParam;
END
But I'm not sure how to link them, before using sps, I had two queries. The outer one retrieved user details and the inner query was executed for each row in the results set of the first i.e. getting the relations array for each user. However when I switched from basic queries to calling stored procedures I get the error that commands are out of sync.
I can't seem to find an example of what I'm trying to achieve. Any help appreciated
UPDATE the php code looks something like this:
$fetch = mysql_query("CALL getUsers();");
while ($row = mysql_fetch_array($fetch)){
$GetRelations_Query = "CALL getRels();";
$relations_results=mysql_query($GetRelations_Query);
$relations_arr = array();
while ($InnerRow = mysql_fetch_array($relations_results)){
$relationsReturned = array('relID' =>$InnerRow['personID'], 'relFName' => $InnerRow['FName'], 'relSName' => $InnerRow['SName'],
'relAddress' => $InnerRow['Address'], 'relPhone' => $InnerRow['Phone'], 'relPhone2' => $InnerRow['Phone2'], 'relType' =>
$InnerRow['relationship']);
array_unshift($relations_arr,$relationsReturned);
}
$row_array = array('userID' => $row['userID'], 'userName' => $row['userName'].' '.$row['userSName'], 'userAddress' => $row['userAddress'],
'userMobile' => $row['userMobile'],'userPhone2' => $row['userPhone2'], 'latitude' => $row['latitude'], 'longitude' => $row['longitude'], 'relations' => $relations_arr);
array_unshift($return_arr,$row_array);
}
and error is Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given referring to the line 'while ($InnerRow = mysql_fetch_array($relations_results)){'
. I know that when I print out the error associated with that query, I am retrieving the error message stated above i.e. commands out of sync