Php 如何在查询结果中重命名sql列名
我有一个基于mysqli查询构建数组的函数:Php 如何在查询结果中重命名sql列名,php,sql,mysqli,Php,Sql,Mysqli,我有一个基于mysqli查询构建数组的函数: if ($type == 'item') { $the_query = "SELECT ID, SKU from store_items WHERE Qty > 0"; } else if ($type == 'brim') { $the_query = "SELECT ID, BrimID from store_item_brims"; } else if ($type == 'condition') { $
if ($type == 'item') {
$the_query = "SELECT ID, SKU from store_items WHERE Qty > 0";
} else if ($type == 'brim') {
$the_query = "SELECT ID, BrimID from store_item_brims";
} else if ($type == 'condition') {
$the_query = "SELECT ID, CondDesc from store_item_conditions";
} else if ($type == 'grade') {
$the_query = "SELECT ID, GradeDesc from store_item_grades";
}
$query = $db->query($the_query);
if ($query->num_rows > 0) {
$results = array();
while ($returned = $query->fetch_assoc()) {
array_push($results, $returned);
}
}
return $results;
例如,如果$type
是项
,我将有一个如下数组:
$array['ID'] // Some ID
$array['SKU'] // The SKU
如果$type
是brim
,我会:
$array['ID'] // Some ID
$array['BrimID] // A brim ID
我试图做的是修改查询,以便无论发生什么情况,结果数组都具有相同的键,即:
$array['ID']
$array['Value']
因此,无论$type
是什么类型,数组始终具有ID
和Value
键
我如何修改这些查询才能做到这一点?只需使用一个别名(最好是一个有意义的别名,而不是像我一样使用的任何别名):
而PHP:
$array['ID']
$array['whatever']
只需使用别名(最好是有意义的别名,而不是像我使用的那样使用任何别名):
而PHP:
$array['ID']
$array['whatever']