我使用CONCAT
来组合多个列,
SELECT CONCAT(col1, ' - ', col2) AS result
FROM table
WHERE col1 LIKE '%apple%' OR
col2 LIKE '%apple%'
ORDER BY col1 ASC
然后作为json数据返回。
while ($row = mysql_fetch_array($query)) {
$data[] = $row['result'];
}
echo json_encode($data);
问题:当我在jQuery中读取数据时,我如何将数据分离到其单独的字段?分开寄是不是更好(更有效率)?
<script>
$(function() {
$( "#AllWord" ).autocomplete({
source: 'search_apple.php'
});
});
</script>
最好是单独发送,所以查询中没有concat。
编码:while ($row = mysql_fetch_array($query)) {
$data[] = $query; // No need to create another array, it is already correct
}
echo json_encode($data);
你仍然可以连接(最好的位置是在查询本身),如果你需要连接,col1和col2的旁边。
最后一点:
请不要使用mysql_*函数。
请使用mysqli_*或PDO。
这里有更多信息:为什么我不应该在PHP中使用mysql_*函数?