Combine PHP prepared statments with LIKE

后端 未结 6 1782
南方客
南方客 2020-12-02 00:12

Anyone know how to combine PHP prepared statements with LIKE? i.e.

\"SELECT * FROM table WHERE name LIKE %?%\";

6条回答
  •  萌比男神i
    2020-12-02 00:58

    For me working great, I've looked for answer hours, thx.

        $dbPassword = "pass";
        $dbUserName = "dbusr";
        $dbServer = "localhost";
        $dbName = "mydb";
    
        $connection = new mysqli($dbServer, $dbUserName, $dbPassword, $dbName);
    
        if($connection->connect_errno)
        {
            exit("Database Connection Failed. Reason: ".$connection->connect_error);
        }
            $tempFirstName = "reuel";
        $sql = "SELECT first_name, last_name, pen_name FROM authors WHERE first_name LIKE CONCAT(CONCAT('%',?),'%')";
        //echo $sql;
    
        $stateObj = $connection->prepare($sql);
        $stateObj->bind_param("s",$tempFirstName);
        $stateObj->execute();
        $stateObj->bind_result($first,$last,$pen);
        $stateObj->store_result();
    
        if($stateObj->num_rows > 0) {
            while($stateObj->fetch()){
                echo "$first, $last \"$pen\"";
                echo '
    '; } } $stateObj->close(); $connection->close();

提交回复
热议问题