提问者:小点点

对于长度超过2个字符的字符串,MySQL LIKE模式返回空结果[closed]


我正在开发一个电子商务网站。 我添加了一个搜索,每当我执行搜索时,它都会返回空结果。

在我的产品表中,我有6个产品包含单词“参议员”。 每当我在搜索表单中输入senator时,它都会返回空结果。

我也尝试搜索“se”,它返回了6个项目。 我不知道我哪里做错了。

总之,如果我搜索长度大于2的字符串,它将返回空结果。

这是我的代码


$q = $_REQUEST[ "q" ];

$sql = "SELECT * FROM products WHERE name LIKE '%{$q}%' ORDER BY id DESC";

if( $DB->numRows( $sql ) > 0 ) {
var_dump( $DB->select( $sql ) );
} else {
echo "No results found";
}

编辑:

下面是“select”和“numrows”方法

class DB {


    private $conn;

    private const HOSTNAME = "localhost";
    private const USERNAME = "root";
    private const PASSWORD = "";
    private const DATABASE = "store";


    //Constructor
    public function __construct() {
        $this->conn = $this->connect();
    }

    //Connect to database
    public function connect() {

        $conn = new mysqli( self::HOSTNAME, self::USERNAME, self::PASSWORD, self::DATABASE );

        //Check connection
        if( mysqli_connect_error() ) {
            $conn_err = "Error: [ <b>" . mysqli_connect_errno() . " </b> ]<br>Unable to connect to DataBase.<br><br>";
            $conn_err .= "Error details: <br><b>" . mysqli_connect_error() . "</b><br>";
            die( $conn_err );
        }
        
        //Set database Charset
        $conn->set_charset( "utf8mb4" );

        return $conn;

    }

    //Select From Database
    public function select( $query, $paramType = "", $paramArray = array() ) {

        if( $stmt = $this->conn->prepare( $query ) ) {

            if( !empty( $paramType ) && !empty( $paramArray ) ) {

                $this->bindQueryParams( $stmt, $paramType, $paramArray );

            }

            $stmt->execute();

            if( $result = $stmt->get_result() ) {

                if( $result->num_rows > 0 ) {

                    while( $row = $result->fetch_assoc() ) {

                        $resultset[] = $row;
                    }
                }
            }

            if( !empty( $resultset ) ) {
                return $resultset;
            }
        }
    }


    //Query Database
    public function query( $query, $paramType = "", $paramArray = array() ) {

        if( $stmt = $this->conn->prepare( $query ) ) {

            if( !empty( $paramType ) && !empty( $paramArray ) ) {

                $this->bindQueryParams( $stmt, $paramType, $paramArray );

            }

            $stmt->execute();
            $stmt->store_result();

            if( !empty( $stmt->insert_id ) ) {
                return TRUE;
            } else {
                return FALSE;
            }

        }
    }


    //Bind Query Params
    public function bindQueryParams( $stmt, $paramType, $paramArray = array() ) {

        $paramValueReference[] = & $paramType;

        for( $i = 0; $i < count( $paramArray ); $i++ ) {

            $paramValueReference[] = & $paramArray[ $i ];
            
        }

        call_user_func_array( array( $stmt, "bind_param" ), $paramValueReference );
    }


    //Get Number of Rows
    public function numRows( $query, $paramType = "", $paramArray = array() ) {

        if( $stmt = $this->conn->prepare( $query ) ) {

            if( !empty( $paramType ) && !empty( $paramArray ) ) {
                $this->bindQueryParams( $stmt, $paramType, $paramArray );
            }

            $stmt->execute();
            $stmt->store_result();
            $rows = $stmt->num_rows;

            return $rows;

        }
    }
}

共1个答案

匿名用户

我在数据库numRows()方法中没有发现任何错误,除了将查询字符串传递给它的方式有一个错误。

我绝对建议你使用事先准备好的陈述。

$q = $_REQUEST["q"];

if (!empty($q)) {
    $db = `database component initialisation...`;

    $term = "%{$q}%";
    $sql = "select * from products where name like ? order by id desc";

    /**
    * Passing query string in addition with param type 's' => string (2nd argument) 
    * and the actual query param for which 
    * the statement should compare against in like clause (3rd argument).
    */
    if ($db->numRows($sql, 's', [$term]) > 0) {
        // processing...
    } else {
        echo "No results found";
    }
}