当在php中使用时,MySQL查询返回null结果

时间:2014-05-30 11:05:11

标签: php mysql

当我在PHP脚本中使用以下Like查询时,我得到null结果

    $MasjidName = $_GET['MasjidName'];
$Percent = "%";
$search = $Percent.$MasjidName.$Percent;

echo $search;

$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '".$search."'";

// get a product from products table
$result = mysql_query($sql) or die(mysql_error());

我也试过以下

$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") or die(mysql_error());

以下是我得到的空结果

{"masjids":[{"MasjidName":null,"Address":null,"Latitude":null,"Longitude":null}],"success":1,"masjid":[]}

以下添加的整个代码是我一直在尝试工作的脚本

   <?php
    $response = array();
    require_once dirname(__FILE__ ). '/db_connect.php';;
    $db = new DB_CONNECT();
    if (isset($_GET["MasjidName"])) 
    {
            $MasjidName = $_GET['MasjidName'];
            $MasjidName = mysql_real_escape_string($MasjidName);  // you have to escape your variable here.
            $sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%$MasjidName%'";
            $result = mysql_query($sql) or die(mysql_error());
            $response["masjids"] = array();

        if (!empty($result)) {
            // check for empty result
            if (mysql_num_rows($result) > 0) {


                while ($row = mysql_fetch_array($result)) {

                $row = mysql_fetch_array($result);

                $masjid = array();
                $masjid["MasjidName"] = $row["MasjidName"];
                $masjid["Address"] = $row["Address"];
                $masjid["Latitude"] = $row["Latitude"];
                $masjid["Longitude"] = $row["Longitude"];

                // success
                $response["success"] = 1;

                // user node
                $response["masjid"] = array();
                array_push($response["masjids"], $masjid);

                }
                // echoing JSON response
                echo json_encode($response);
            } else {
                // no product found
                $response["success"] = 0;
                $response["message"] = "No product found";

                // echo no users JSON
                echo json_encode($response);
            }
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // required field is missing
        $response["success"] = 0;
        $response["message"] = "Required field(s) is missing";

        // echoing JSON response
        echo json_encode($response);
    }
    ?>

2 个答案:

答案 0 :(得分:0)

试试这个:

$MasjidName = $_GET['MasjidName'];
$MasjidName = mysql_real_escape_string($MasjidName);  // you have to escape your variable here.
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%$MasjidName%'";
$result = mysql_query($sql) or die(mysql_error());

答案 1 :(得分:0)

尝试使用

$sql = "SELECT * FROM MasjidMaster WHERE MasjidName LIKE '".$search."'";

我试过了SELECT * FROM Customers WHERE City LIKE 's%';,它给了我非常好的结果。但是当我尝试SELECT * FROM 'Customers' WHERE 'City' LIKE 's%';时,它给了我一个空结果。

只需删除''并尝试一下即可。希望它有所帮助。