I write a code to fetch data from Mysql Database with union to select multiple data from different table the result is ok but the column name is the same name
<?php
$objConnect = mysql_connect("localhost","xxxxx","xxxxx") or die(mysql_error());
$objDB = mysql_select_db("musicstore");
$strSQL = "select count(song_Id) as 'cnts' from song where date_rel = CURDATE()
union select count(album_id) as 'cnta' from album ";
$objQuery = mysql_query($strSQL) or die (mysql_error());
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
mysql_close($objConnect);
echo json_encode($resultArray);
?>
the result is [{"cnts":"38"},{"cnts":"3"}]
I don't know why it's doesn't get a column name from defined name
Add dummy columns to achieve this.
$strSQL = " select count(song_Id) as 'cnts', 'cnta' from song where date_rel = CURDATE()
union select 'cnta', count(album_id) as 'cnta' from album " ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.