KeepCool KeepCool - 1 month ago 8
PHP Question

Having trouble checking if MySQL query returned anything

Here's what I'm doing.


  1. I'm checking if there's a "version" value in the URL with $get_version.

  2. Get the latest version from the database and set as a default variable.

  3. If the URL variable is good, check the database to see if it exists then set the appropriate variables.

  4. If doesn't exist, use default value from above.



It always goes to the "Bad query section". Either my query is wrong or my if statement doesn't work.

Here's my code. Also, is there a cleaner way of doing it?

// Check if there's a version in URL. If not, set to empty.
$get_version = isset($_GET['version']) ? $_GET['version'] : '';


// Set defaults if nothing in URL
$query = "SELECT * FROM sn_hockey_versions ORDER BY version_id DESC LIMIT 1";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);

while ($row = mysqli_fetch_array($result)) {
$newest_version_id = $row['version_id'];
$newest_sections = $row['sections'];
}

if (!empty($get_version) && preg_match('/^[0-9.]*$/', $get_version)) {

$query = "SELECT version_id, sections FROM sn_hockey_versions WHERE version = '".$get_version."'";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);


if ($row = mysqli_fetch_array($result)) {

$set_version = $row['version_id'];
$v_sections = $row['sections'];
$test = "IT WORKS!!!!";

}
else {
$set_version = $newest_version_id;
$v_sections = $newest_sections;
$test = "Bad query";
}
}
else {
$set_version = $newest_version_id;
$v_sections = $newest_sections;
$test = "Set default";
}

Answer

Your conditional if statement is checking to see whether $rows is set to mysql_fetch_array($result), not whether it returned any results. If the query returns results, the conditional statement returns true, $row is set to the resulting array, and your if block will be evaluated. Otherwise, $row is set to null, making the condition false, and the else block evaluates.

Since your else statement is evaluating, this leads me to believe that there is an issue with the query, which can be tested by printing out the results of the array. While there are numerous ways to check if a query returns any results, to prevent confusion in your code, checking the value of mysql_num_rows would be a better solution before fetching the results:

  if (mysqli_num_rows($result) > 0) {
      $row = mysqli_fetch_array($result)

For more information about mysqli_num_rows check out http://php.net/manual/en/mysqli-result.num-rows.php

Also see: Whats the proper way to check if mysql_query() returned any results?

The other recommendation I have for making the code more efficient is: Only query the database for the default version when necessary. Too many unnecessary queries can lead to database performance issues. One way to accomplish this, is to place the default version query into a function and call it only in the "bad query" "set default" blocks. I hope this helps.

Comments