我是存储过程的新手,并且正在使用mysql。我在phpmyadmin中创建过程。
我需要实现的是一个存储过程,它将返回一个表中的所有用户详细信息及其在链接表中的关联关系。
我需要我的php web服务来调用该过程并能够返回类似于以下内容的json:
{
"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"
}
]
}
我目前有两个存储过程,即
getUsers:
BEGIN
SELECT * FROM Users ORDER BY userSName DESC;
END
和getRels:
BEGIN
SELECT * FROM relations WHERE relations.userID = UserIDParam;
END
但我不确定如何链接它们,在使用sps之前,我有两个问题。外部检索用户细节,并且对第一个结果集中的每一行执行内部查询,即获得每个用户的关系数组。但是,当我从基本查询切换到调用存储过程时,我收到命令不同步的错误。
我似乎无法找到一个我想要实现的例子。任何帮助表示赞赏
更新 php代码看起来像这样:
$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);
}
并且错误是警告:mysql_fetch_array()期望参数1是资源,参考行'while ($InnerRow = mysql_fetch_array($relations_results)){'
给出布尔值。我知道当我打印出与该查询相关的错误时,我正在检索上述错误消息,即命令不同步