简体   繁体   English

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

[英]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. 我正在检查$ get_version在URL中是否有“版本”值。
  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. 如果URL变量正确,请检查数据库以查看其是否存在,然后设置适当的变量。
  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. 我的查询错误或if语句不起作用。

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

To check if it was successful outside of the code, copy the query and run it in phpmyadmin or through ssh to see if it returns results. 要检查代码外是否成功,请复制查询并在phpmyadmin或ssh中运行以查看其是否返回结果。 If it returns results then put in some stop checks in your code to see what you are getting. 如果返回结果,则在代码中进行一些停止检查以查看得到的结果。 like so 像这样

 echo '<pre>jwow',print_r($result,1),'</pre>';
 die('here');

Just place that under which ever result you would like to check. 只需将要检查的结果放在下面即可。 I like to name the query results by different names. 我喜欢用不同的名字来命名查询结果。 like $defaults_results and $version_results . 如$ defaults_results和$ version_results。 if it is getting to "Bad Query" you will have found your error after trying that. 如果遇到“错误查询”,您将在尝试后发现错误。 If it returns no results in phpmyadmin then read the errors it gives. 如果在phpmyadmin中没有返回任何结果,则读取它给出的错误。

Your conditional if statement is checking to see whether $rows is set to mysql_fetch_array($result), not whether it returned any results. 您的条件if语句将检查$ rows是否设置为mysql_fetch_array($ result),而不是是否返回任何结果。 If the query returns results, the conditional statement returns true, $row is set to the resulting array, and your if block will be evaluated. 如果查询返回结果,则条件语句返回true,将$ row设置为结果数组,并将评估if块。 Otherwise, $row is set to null, making the condition false, and the else block evaluates. 否则,将$ row设置为null,使条件为false,然后对else块求值。

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. 由于您的else语句正在评估,因此使我相信查询存在问题,可以通过打印出数组结果进行测试。 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: 尽管有很多方法可以检查查询是否返回任何结果,但是为了避免代码混乱,在获取结果之前检查mysql_num_rows的值将是一个更好的解决方案:

  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 有关mysqli_num_rows的更多信息,请访问http://php.net/manual/en/mysqli-result.num-rows.php

Also see: Whats the proper way to check if mysql_query() returned any results? 另请参见: 检查mysql_query()是否返回任何结果的正确方法是什么?

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. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM