Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

html - MySQL like Query fails in PHP

   $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);
    }
    ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
$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%')


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...