php - POSTGRESQL WHERE query for multiple columns
Get the solution ↓↓↓I want to make a query which will search across four columns.
column names are:
cn00 | cn01 | cn03 | cn12
I have tried this:
$load_elements = "cn00,cn01,cn03,cn12,cn22,cn23,cn29,cn30,cn46,cn49";
$search_keys_p = $_REQUEST['keyword'];
$search_keys = $search_keys_p;
$search_keys = escape($search_keys);
$sql = "SELECT ".$load_elements." FROM cont_tab WHERE (cn00|cn01|cn03|cn12) LIKE :keyword order by cn46 asc";
$statement = $connection->prepare($sql);
$search_keys = "%".$search_keys."%";
$statement->bindValue(':keyword', $search_keys, PDO::PARAM_STR);
$statement->execute();
But it is not working out, Can anyone help me out with this......
Answer
Solution:
Sorry, but with some tries I have found the answer.
I have searched and worked on this for 3 hours and did not get a proper answer anywhere so I posted it here and luckily have found the answer myself.
ADD & MODIFY THE BELOW CODE TO THE QUESTION
$columns = " concat_ws(' ', cn00,cn02,cn03,cn12)";
$sql = "SELECT ".$load_elements." FROM cont_tab WHERE(".$columns."LIKE :keyword) order by cn46 asc";
It might help some beginner like me...
$load_elements = "cn00,cn01,cn03,cn12,cn22,cn23,cn29,cn30,cn46,cn49";
$search_keys_p = $_REQUEST['keyword'];
$search_keys = $search_keys_p;
$search_keys = escape($search_keys);
$columns = " concat_ws(' ', cn00,cn02,cn03,cn12)";
$sql = "SELECT ".$load_elements." FROM cont_tab WHERE(".$columns."LIKE :keyword) order by cn46 asc";
$statement = $connection->prepare($sql);
$search_keys = "%".$search_keys."%";
$statement->bindValue(':keyword', $search_keys, PDO::PARAM_STR);
$statement->execute();
PS: Any other easy methods to solve this differently are welcome
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: undefined array key php
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.