I am a little new to php, and I am trying to retrieve a raw from the database and the result is always empty. I have used the following code to insert a record in the table and it worked.
INSERT INTO `registeration`.`cars` (`lnumber`, `type`, `model`, `motor`, `owner`
,`expdate`) VALUES ('123654', 'Nissan maxima', '2008',
'1500', 'name name', '2013-11-30');
The result was 1 raw inserted
The code used in the php file is:
if (isset($_GET["lnumber"])) {
$lnumber = $_GET['lnumber'];
echo $lnumber;
$result = mysql_query("SELECT * FROM cars WHERE lnumber = $lnumber");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
....code....
}
} else {
// no car found
$response["success"] = 0;
$response["message"] = "No cars 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);
}
However, the results are always: 123654{"success":0,"message":"No cars found"}
mysql_errortell you? Also: Please, don't usemysql_*functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.empty()to check the query. The result will never be "empty" (false) unless the query has an error. I would removeif (!empty($result)) {and just checkmysql_num_rowsas you are doing.