我根据search
制作了productName
,因此每当用户搜索例如product A
时,它就会显示产品A,但如果它搜索的产品我没有在我的数据库中,它仍应显示all the products
。
我的first query
works fine
,但我的second query
doesn´t display any products
,当我console the response on the client
时,我得到empty array
。问题在哪里?
这是我的搜索API:
<?php
try {
// connect to the database
require 'connect.php';
// data from the BROWSER
$sSearch = $_GET['search'];
// TURN it into UPPERCASE
strtoupper( $sSearch );
// create a query: select products where search value equals productName
$query1 = $conn->prepare("SELECT * FROM products WHERE productName=:productName");
$query1->bindParam( ':productName' , $sSearch );
$bResult1 = $query1->execute();
$ajResult1 = $query1->fetchAll(PDO::FETCH_ASSOC);
$sajResult1 = json_encode( $ajResult1 );
// create another query: select all products if search does not match any productName
$query2 = $conn->prepare("SELECT * FROM products");
$query2->execute();
$ajResult2 = $query2->fetchAll(PDO::FETCH_ASSOC);
$sajResult2 = json_encode( $ajResult2 );
$sjResponse = $bResult1 ? $sajResult1 : $sajResult2;
echo $sjResponse;
} catch (Exception $e) {
echo "ERROR";
}
?>