我知道这对大多数人来说可能是一个平凡的问题,虽然我很难找到一个我可以借鉴的例子。我有这个功能来检索一些所有者的详细信息。在某些情况下,它将检索两个联系人。我希望将数组中保存的第二组详细信息保存为$ output [' owner_id1'] ...等,但不确定如何完成。
function retrieve_owner_details($propertyid){
global $connection;
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "select * FROM owner WHERE property_id='$propertyid' LIMIT 2";
$result = mysqli_query($connection, $query);
while($owner = mysqli_fetch_assoc($result)) {
$output['owner_id'] = $owner['owner_id'];
$output['owner_name'] = $owner['owner_name'];
$output['owner_email'] = $owner['owner_email'];
$output['owner_mobile'] = $owner['owner_mobile'];
$output['owner_phone'] = $owner['owner_phone'];
}
mysqli_free_result($result);
return $output;
}...
答案 0 :(得分:2)
我更喜欢由$output
编制索引的$owner
owner_id
数组的嵌套 function retrieve_owner_details($propertyid){
global $connection;
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "select * FROM owner WHERE property_id='$propertyid' LIMIT 2";
$result = mysqli_query($connection, $query);
$output=array();
while($owner = mysqli_fetch_assoc($result)) {
$output[$owner['owner_id']]=$owner;
}
mysqli_free_result($result);
return $output;
}...
数组。它更通用,代码更少:
{{1}}
答案 1 :(得分:1)
你可以替换"而#34;对于2"如果" :
function retrieve_owner_details($propertyid){
global $connection;
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "select * FROM owner WHERE property_id='$propertyid' LIMIT 2";
$result = mysqli_query($connection, $query);
if($owner = mysqli_fetch_assoc($result)) {
$output['owner_id'] = $owner['owner_id'];
$output['owner_name'] = $owner['owner_name'];
$output['owner_email'] = $owner['owner_email'];
$output['owner_mobile'] = $owner['owner_mobile'];
$output['owner_phone'] = $owner['owner_phone'];
}
if($owner = mysqli_fetch_assoc($result)) {
$output['owner_id1'] = $owner['owner_id'];
$output['owner_name1'] = $owner['owner_name'];
$output['owner_email1'] = $owner['owner_email'];
$output['owner_mobile1'] = $owner['owner_mobile'];
$output['owner_phone1'] = $owner['owner_phone'];
}
mysqli_free_result($result);
return $output;
}...