0

I am new to PHP development and I current have a working search query by using MySQLI. I would like to convert my statement to switch from mySQLI to PDO but I am confused how to inject a variable into the PDO query as I used to do :

$search = mysqli_real_escape_string($connect, $_POST["query"]);

where connect was :

$connect = mysqli_connect("localhost", "root", "", "app")

I am currently getting this warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, which means that there is something wrong with my query:

try{
    $conn = new PDO("mysql:host=$servername;dbname=app", $username, $password,[
        PDO::ATTR_EMULATE_PREPARES => false, 
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);
} catch (PDOException $e){
    echo "Connection failed: " . $e->getMessage();
}

if(isset($_GET["query"])){
    echo "hello world";

    $search = $_GET["query"];

    $query = $conn->prepare("SELECT * FROM cities WHERE city LIKE search= ? LIMIT 10");

    $query->execute([':search' =>  $search]); 


    $result = $query->fetchAll(\PDO::FETCH_ASSOC); 

    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
            $queryResult = '<li> .$row["population"] </li>';
        }
    }
    echo $queryResult;
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Gerard1999
  • 41
  • 3

0 Answers0