Query on more than one mysql column with PHP

0

I do this to query the Mysql database

$keyword = strval($_POST['query']);
    $search_param = "{$keyword}%";
    $conn =new mysqli('xxx', 'xxx', 'xxx' , 'xxx');

    $sql = $conn->prepare("SELECT * FROM empresas WHERE nome LIKE ? or palavras_chave LIKE ?")
    $sql->bind_param("s",$search_param);            
    $sql->execute();
    $result = $sql->get_result();
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
        $countryResult[] = $row["nome"];
        }
        echo json_encode($countryResult);
    }
    $conn->close();

Just looking for the name, how would I look for another mysql column? Such as name and keywords .

    
asked by anonymous 16.04.2018 / 21:31

1 answer

1

You can add conditions and and or .

Example:

$sql = $conn->prepare("SELECT * FROM empresas WHERE nome  LIKE ? and palavras-chaves LIKE ?");

or

$sql = $conn->prepare("SELECT * FROM empresas WHERE nome  LIKE ? or palavras-chaves LIKE ?");

The choice will depend on your specific case.

At the time of replacing the ? with the parameters, be sure to set % before and after the value of the variables.

Example:

SELECT * FROM empresas WHERE nome  LIKE "%$nome%"

try this code:

$keyword = strval($_POST['query']);
$search_param = "%$keyword%";
$conn =new mysqli('xxx', 'xxx', 'xxx' , 'xxx');

$sql = $conn->prepare("SELECT * FROM empresas WHERE nome LIKE ? or palavras_chave LIKE ?")
$sql->bind_param("ss",$search_param, $search_param); 

$sql->execute();
$result = $sql->get_result();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    $countryResult[] = $row["nome"];
    }
    echo json_encode($countryResult);
}
$conn->close();
    
16.04.2018 / 21:35