0

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"}

4
  • 5
    Well, what does mysql_error tell you? Also: Please, don't use mysql_* 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. Commented Apr 26, 2013 at 18:26
  • 4
    warning your code is vulnerable to sql injection attacks! Commented Apr 26, 2013 at 18:26
  • 1
    I don't think you want to use empty() to check the query. The result will never be "empty" (false) unless the query has an error. I would remove if (!empty($result)) { and just check mysql_num_rows as you are doing. Commented Apr 26, 2013 at 18:28
  • 1
    Since you say you're new to PHP, I feel I need to let you know that your code is very poor quality. That's not necessarily your fault, but the fault of the tutorials (or whatever) you've been following to learn PHP -- you're using out-of-date techniques, and you're writing code that is insecure. If you want to learn how to write good quality PHP code, you need to read good quality and up-to-date tutorials. Here's a good one on writing modern database code to get you started: phpmaster.com/avoid-the-original-mysql-extension-2 Commented Apr 26, 2013 at 18:30

3 Answers 3

1

Your code is incorrect. The mysql function return boolean false on FAILURE, e.g. when an error occurs. mysql_query either return a result handle (query ran successfully) or a boolean FALSE (something blew up).

A result set which has no rows is NOT an error. It's simply an empty set. The code should be more like

$result = mysql_query($sql) or die(mysql_error()); // handle any error conditions
if (mysql_num_rows($result) == 0) {
  echo 'no records found';
} else {
  ... build your json here ...
}

And as others have said above, the mysql_*() functions are obsolete and deprecated. You should be using mysqli or PDO instead.

And again as others have said, you are vulnerable to SQL injection attacks. Read up and learn about how to prevent those before you do any more work with PHP/sql

Sign up to request clarification or add additional context in comments.

Comments

0

First, you'll want to look into moving away from mysql_* and into mysqli_* or PDO. Second, you need to fetch the information appropriately:

$result = mysql_query("SELECT * FROM cars WHERE lnumber = '$lnumber'");

if(mysql_num_rows($result) > 0)
{
    while($row = mysql_fetch_array($result))
    {
        echo $row['car'];
    }
}
else
{
    // No cars.
}

Then do whatever it is you're going to do with the database information.

Comments

0

Just remove this test if (!empty($result)) and add die(mysql_error());

$result = mysql_query("SELECT * FROM cars WHERE lnumber = $lnumber") or die(mysql_error());

// 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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.