I am currently developing an application that will be communicating with a database via a PHP service I have written.
The service I am having problems with is one that should look for a row or rows in a table based on a search string from the users.
The PHP below is designed to recieve a GET request with a variable of "name" which will be the data the SQL query uses. I cannot see anything wrong with my code however the rows returned from a search is always 0.
// checks for the post data
if (isset($_GET["name"])) {
$name = '%' . $_GET['name'] . '%';
// get a list of products from the database
$result = mysql_query("SELECT * FROM products WHERE name LIKE $name");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$products = array();
$products["id"] = $row["id"];
$products["name"] = $row["name"];
$products["type"] = $row["type"];
$products["price"] = $row["price"];
$products["photo"] = $row["photo"];
// success
$response["success"] = 1;
// products node
$response["products"] = array();
array_push($response["products"], $products);
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no products JSON
echo json_encode($response);
}
} else {
// no products found
$response["success"] = 0;
$response["message"] = "Search Complete... No products found";
// echo no products 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);
}
I have an entry in the table it is looking at and a name of "crisps", so when I send a get request with data of say "cr" i would expect to see that entry in the results, however it returns 0 rows.
Then whats even stranger is when I run the SQL below directly against my database it actually pulls back the correct record.
SELECT * FROM products WHERE name LIKE "%cr%"
Any ideas??