這裏我試圖從四個表中獲取數據。我成功地獲得了結果。但是,我需要獲取過濾結果的表名。例如,我得到了10個結果。來自TBL_CAR的3個結果,來自TBL_BIKE的3個結果,來自TBL_TRUCK的2個結果,來自TBL_BUS的2個結果。我想將這些不同的鏈接應用於這些結果。獲取數據庫表名作爲列名
如何將數據庫表名稱作爲列名將不同的url鏈接應用於這些結果?
$sql = "(SELECT model_name, maker_url, model_url FROM ".TBL_CAR_ADD_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)
UNION
(SELECT model_name, maker_url, model_url FROM ".TBL_BIKE_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)
UNION
(SELECT model_name, maker_url, model_url FROM ".TBL_TRUCK_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)
UNION
(SELECT model_name, maker_url, model_url FROM ".TBL_BUS_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)";
$res = mysql_query($sql, $CN);
$rows = array();
while($row = mysql_fetch_array($res))
{
if(resutls FROM TBL_CAR)
{
$rows[] = array('url' => asort_get_url(CAR_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_model_image_path("thumb", $row['maker_url'],$row['model_url']));
}
elseif(results FROM TBL_BIKE)
{
$rows[] = array('url' => asort_get_url(BIKE_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bike_model_image_path("thumb", $row['maker_url'],$row['model_url']));
}
elseif(results FROM TBL_TRUCK)
{
$rows[] = array('url' => asort_get_url(TRUCK_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_truck_model_image_path("thumb", $row['maker_url'],$row['model_url']));
}
elseif(resutls FROM TBL_BUS)
{
$rows[] = array('url' => asort_get_url(BUS_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bus_model_image_path("thumb", $row['maker_url'],$row['model_url']));
}
}
echo stripslashes(json_encode($rows,JSON_PRETTY_PRINT));
此代碼是有點可怕,我會鼓勵你考慮遷移到[PDO](HTTP:// PHP .net/PDO_mysql)而不是mysql_ *;儘管讓代碼自動更好,但這不是一個神奇的項目,但我認爲如果您開始閱讀使用PDO的代碼,並比較它如何執行操作,可能會幫助您找到更好的方法來構建結構。 [Here's](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)一個快速教程,幫助你開始。 – TML 2014-10-09 04:44:28