dongtu0088 2016-10-12 17:24
浏览 9
已采纳

无法检查MySQL查询是否返回任何内容

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";
    }
  • 写回答

2条回答 默认 最新

  • duanjiao8871 2016-10-12 18:00
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 angular开发过程中,想要读取模型文件,即图1的335行,会报404错误(如图2)。但我的springboot里配置了静态资源文件,如图3。且在该地址下我有模型文件如图4,请问该问题该如何解决呢?
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题