gpt4 book ai didi

php - 在 php 中使用时,MySQL 查询返回空结果

转载 作者:行者123 更新时间:2023-11-29 03:34:52 24 4
gpt4 key购买 nike

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

    $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);
}
?>

最佳答案

试试这个:

$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());

关于php - 在 php 中使用时,MySQL 查询返回空结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23953086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com