Php 多个mysql查询导致空返回

Php 多个mysql查询导致空返回,php,mysql,Php,Mysql,在使用两个mysql查询执行PHP函数时,第二个函数返回null,这不应该有,因为它们都在phpmyadmin SQL查询提交中工作 以下是函数: public static function getStates($countryId) { try { $query = "SELECT * FROM `Apostas` WHERE `idConfronto` = ".$countryId; $result = dbconfig::run($quer

在使用两个mysql查询执行PHP函数时,第二个函数返回null,这不应该有,因为它们都在phpmyadmin SQL查询提交中工作

以下是函数:

  public static function getStates($countryId) {
     try {
       $query    = "SELECT * FROM `Apostas` WHERE `idConfronto` = ".$countryId;
       $result = dbconfig::run($query);
       if(!$result) {
         throw new exception("Confronto invalido.");
       }
       $res = array();
       while($resultSet = mysqli_fetch_assoc($result)) {
        $sql2   = "SELECT * FROM `Mercados` WHERE `idMercado` = ".$resultSet["idMercado"];
        $result2 = dbconfig::run($sql2);
        if(!$result2) {
            throw new exception("Não há mercados.");
        }
        while ($row2 = mysqli_fetch_assoc($result2)) {
            $res[$resultSet['idMercado']] = $row2['nomeMercado'];
        }
       }
       $data = array('status'=>'success', 'tp'=>1, 'msg'=>"States fetched successfully.", 'result'=>$res);
     } catch (Exception $e) {
       $data = array('status'=>'error', 'tp'=>0, 'msg'=>$e->getMessage());
     } finally {
        return $data;
     }
   }
第一个查询每次都有效,但第二个查询指向异常“Não hámercados”

调试该函数后,变量$resultSet[“idMercado”]起作用,sql查询也起作用,但代码导致异常

我做错了什么?也许是语法方面的问题

--编辑1: 根据请求,dbconfig::run的代码:

  public static function run($query) {
    try {
      if(empty($query) && !isset($query)) {
        throw new exception("Query string is not set.");
      }
      $result = mysqli_query(self::$con, $query);
      self::close();
     return $result;
    } catch (Exception $e) {
      echo "Error: ".$e->getMessage();
    }

  } 
--编辑2: 正如Cavid所建议的,在关闭连接之前进行所有查询,下面是函数的结果:

public static function getStates($countryId) {
     try {
       $query    = "SELECT * FROM `Apostas` WHERE `idConfronto` = ".$countryId;
       $result = $conn->query($query);
       if(!$result) {
         throw new exception("Confronto invalido.");
       }
       $res = array();
       if ($result->num_rows > 0) {
        while ($resultSet = $result->fetch_assoc()) {
            $sql2   = "SELECT * FROM `Mercados` WHERE `idMercado` = ".$resultSet['idMercado'];
            $result2 = $conn->query($sql2);
            if ($result2->num_rows > 0) {
                while ($row2 = $result2->fetch_assoc()) {
                    $res[$resultSet['idMercado']] = $row2['nomeMercado'];
                }
            }
           }
       }
       $data = array('status'=>'success', 'tp'=>1, 'msg'=>"States fetched successfully.", 'result'=>$res);
     } catch (Exception $e) {
       $data = array('status'=>'error', 'tp'=>0, 'msg'=>$e->getMessage());
     } finally {
        return $data;
     }
     $conn->close();
   }

它现在给了我一个错误代码500“Internal server error”

而不是进入两个不同的表,这两个表都有一个引用id,我建议您加入它们,并且只使用一个while循环:

public static function getStates($countryId) {
    try {
        // Inner join both tables
        $query    = "SELECT a.idMercado, a.idConfronto, b.nomeMercado FROM Apostas AS a ";
        $query    .= "INNER JOIN ";
        $query    .= "Mercados AS b ";
        $query    .= "ON a.idMercado = b.idMercado ";
        $query    .= "WHERE a.idConfronto = " . $countryId;
        $result = dbconfig::run($query);
        if(!$result) {
            throw new exception("Confronto invalido.");
        }
        // Results
        $res = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $res[$row['idMercado']] = $row['nomeMercado'];
        }
        $data = array('status'=>'success', 'tp'=>1, 'msg'=>"States fetched successfully.", 'result'=>$res);
    } catch (Exception $e) {
        $data = array('status'=>'error', 'tp'=>0, 'msg'=>$e->getMessage());
    } finally {
        return $data;
    }
}

我建议您加入两个表,并且只使用一个while循环,而不是进入两个表中都有引用id的两个不同表:

public static function getStates($countryId) {
    try {
        // Inner join both tables
        $query    = "SELECT a.idMercado, a.idConfronto, b.nomeMercado FROM Apostas AS a ";
        $query    .= "INNER JOIN ";
        $query    .= "Mercados AS b ";
        $query    .= "ON a.idMercado = b.idMercado ";
        $query    .= "WHERE a.idConfronto = " . $countryId;
        $result = dbconfig::run($query);
        if(!$result) {
            throw new exception("Confronto invalido.");
        }
        // Results
        $res = array();
        while ($row = mysqli_fetch_assoc($result)) {
            $res[$row['idMercado']] = $row['nomeMercado'];
        }
        $data = array('status'=>'success', 'tp'=>1, 'msg'=>"States fetched successfully.", 'result'=>$res);
    } catch (Exception $e) {
        $data = array('status'=>'error', 'tp'=>0, 'msg'=>$e->getMessage());
    } finally {
        return $data;
    }
}


您是否有来自
dbconfig::run
的代码块。。。因为它可能不处理多个查询。只是不要抛出异常,如果结果为空,则添加空数组。还可以在sql2查询中用单引号括起
$resultSet[“idMercado”]
,因此如果它为空,则不会中断SQL。为什么在查询后关闭连接?每次查询后,它都会关闭。毕竟,它应该在页面末尾关闭queries@Randall我添加了块作为编辑,也做了单引号,Cavid我做了,只是没有抛出异常,但还没有从查询中返回数据,我将尝试在所有查询之后关闭的另一个mysql连接。您是否有来自
dbconfig::run
的代码块。。。因为它可能不处理多个查询。只是不要抛出异常,如果结果为空,则添加空数组。还可以在sql2查询中用单引号括起
$resultSet[“idMercado”]
,因此如果它为空,则不会中断SQL。为什么在查询后关闭连接?每次查询后,它都会关闭。毕竟,它应该在页面末尾关闭queries@Randall我添加了块作为编辑,也做了单引号,Cavid我做了,只是没有抛出异常,但还没有从查询中返回数据,我将尝试在所有查询之后关闭另一个mysql连接。只是缺少几个“;”,我曾想过使用内部连接,但我的实现不起作用,我运行了您的代码,它没有显示任何错误,但没有显示结果,这可能是javascript问题,如果是,我将尝试修复它并给出答案tick@GuilhermeGarciadaRosa删除“while”中的“2”:$row=mysqli\u fetch\u assoc($result)我做了一个简单的console.log(数据);在我的javascript函数中,为了查看返回的内容,它给了我一个空数组,我在phpmyadmin sql中测试了代码,它成功了,为什么它不返回数组?没错,id确实遗漏了一些“;”我的坏呵呵,刚刚编辑了我的帖子。工作!!谢谢,只是少了几个“;”,我曾想过使用内部连接,但我的实现不起作用,我运行了您的代码,它没有显示任何错误,但没有显示结果,这可能是javascript问题,如果是,我将尝试修复它并给出答案tick@GuilhermeGarciadaRosa删除“while”中的“2”:$row=mysqli\u fetch\u assoc($result)我做了一个简单的console.log(数据);在我的javascript函数中,为了查看返回的内容,它给了我一个空数组,我在phpmyadmin sql中测试了代码,它成功了,为什么它不返回数组?没错,id确实遗漏了一些“;”我的坏呵呵,刚刚编辑了我的帖子。工作!!非常感谢。