I am searching for words that contain "un" from two columns in a database table. How can I get both row data in the output $data?
Example:
col1 | col2
tom -------- unfriendly
train ------- fast
unused--- cloth
.
$query = mysql_query("SELECT * FROM table
WHERE col1 LIKE '%un%'
OR col2 LIKE '%un%'
ORDER BY col1 ASC");
while ($row = mysql_fetch_array($query)) {
$data[] = $row['col1'];
}
I get $data = [tom,unused] from the above code. How can I get $data = [ tom - friendly, unused - cloth] ?
As @TimBegeleisen said, you can use CONCAT
in your SQL request, but you could also concat your string in your PHP code :
$query = mysql_query("SELECT * FROM table
WHERE col1 LIKE '%un%'
OR col2 LIKE '%un%'
ORDER BY col1 ASC");
while ($row = mysql_fetch_array($query)) {
$data[] = $row['col1'] . ' - ' . $data['col2'];
}
Try using CONCAT
:
SELECT CONCAT(col1, ' - ', col2) AS result
FROM table
WHERE col1 LIKE '%un%' OR
col2 LIKE '%un%'
ORDER BY col1 ASC
And then you would access the result set in your PHP code via:
while ($row = mysql_fetch_array($query)) {
$data[] = $row['result'];
}
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.