MySQL中的查询在PHP中失败 [英] MySQL like Query fails in PHP
问题描述
$ result = mysql_query(SELECT * FROM MasjidMaster WHERE MasjidName LIKE('%moh%'))或者mysql_error();
我得到的错误是
<$ p $第24行中的/home/maximtec/public_html/masjid_folder/MasjidFinderScripts/find_by_name.php中的语法错误,意外的T_STRING
$ p $>解析错误:语法错误, p>
当我在MySQL中使用它时,这个查询不起作用,但是当我把它放在一个PHP脚本中时,它不会工作
请建议一个解决方案
------------编辑:从收到的答案更改查询后-------------------- -----------------
嗯,我更新了我的查询,但现在我得到空结果
{masjids:[{MasjidName:null,Address:null,Latitude:null,Longitude:null}] ,success:1,masjid:[]}
以下是我的完整脚本: / p>
<?php
/ *
*以下代码将获得单个产品详细信息
*产品由产品标识(pid)
* /
标识//数组用于JSON响应
$ response = array();
//包含db连接类
// require_once __DIR__。 /db_connect.php;
require_once dirname(__ FILE__)。 /db_connect.php ;;
//连接到db
$ db = new DB_CONNECT();
//检查发布数据
if(isset($ _ GET [MasjidName])){
$ MasjidName = $ _GET ['MasjidName'];
//从产品表中获取产品
$ result = mysql_query(SELECT * FROM`MasjidMaster` WHERE`MasjidName` LIKE'%moh%')or die (mysql_error());
$ response [masjids] = array();如果(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] = 1;
//用户节点
$ response [masjid] = array();
array_push($ response [masjids],$ masjid);
// array_push($ response [masjid],$ masjid);
}
//回显JSON响应
echo json_encode($ response);
} else {
//找不到产品
$ response [success] = 0;
$ response [message] =未找到产品;
// echo无用户JSON
echo json_encode($ response);
}
} else {
//找不到产品
$ response [success] = 0;
$ response [message] =未找到产品;
// echo无用户JSON
echo json_encode($ response);
}
} else {
//必填字段丢失
$ response [success] = 0;
$ response [message] =必填字段丢失;
//回显JSON响应
echo json_encode($ response);
}
?>
$ result = mysql_query(SELECT * FROM`MasjidMaster` WHERE`MasjidName` LIKE'%moh%')or die(mysql_error());
稍微调整一下,以便在``中实现好的练习包名表和表列。
你也不需要()周围('%moh%')
$result = mysql_query("SELECT * FROM MasjidMaster WHERE MasjidName LIKE ('%moh%')") or die mysql_error();
The error i get is
Parse error: syntax error, unexpected T_STRING in /home/maximtec/public_html/masjid_folder/MasjidFinderScripts/find_by_name.php on line 24
This query does work when i use it in MySQL but it doesn't when I place it in a PHP Script Please suggest a solution
------------EDIT :After changing query from the received answers-------------------------------------
Well I updated my query but now I am getting null results
{"masjids":[{"MasjidName":null,"Address":null,"Latitude":null,"Longitude":null}],"success":1,"masjid":[]}
Following is my full script :
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// include db connect class
//require_once __DIR__ . '/db_connect.php';
require_once dirname(__FILE__ ). '/db_connect.php';;
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["MasjidName"])) {
$MasjidName = $_GET['MasjidName'];
// get a product from products table
$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") 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);
// array_push($response["masjid"], $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);
}
?>
$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") or die(mysql_error());
A little more tweaking, for good practice wrap table names and table columns in ``.
You also shouldn't need () around ('%moh%')
这篇关于MySQL中的查询在PHP中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!