Unable to obtain mysqli PHP Error

0
  

I'm using the global function $ with; and the error went but I do not know if   it's okay to use it in each function and if PHP5 works the same   well that in PHP7

Error:

  

Warning: mysqli_query (): Could not fetch mysqli in C: \ xampp \ htdocs \ new \ trending.php on line 19

     

Warning: mysqli_num_rows () expects parameter 1 to be mysqli_result, null given in C: \ xampp \ htdocs \ new \ trending.php on line   21

code:

$sql = mysqli_query($con,"Select * From trending, ORDER BY top DESC LIMIT 13");

if (mysqli_num_rows($sql) == 0)
    {
        for ($n=1;$n<=12;$n++){
            echo'<div class="div_css_one">';
            echo'#</div>';
            echo'<div class="div_css_one_b">';
            echo'</div>';
        }
    }else{  

        while($data = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
            echo '<li><a href="hashtag?hashtag='.$data['hashtag'].'"><p class="font-weight">#'.$data['hashtag'].'</p></a><spam class="trending_post_top">'.$data['top'].' posts</spam><li>';
        }
    }
    
asked by Shareiv 13.09.2017 в 21:44
source

2 answers

1

It is necessary a total control of the code so that you can know where it is failing:

//Controlar conexión
if (!$con) 
{ 
    echo "Error: No se pudo conectar a MySQL.";

}else{

    //Controlar consulta
    if (mysqli_query($con,"Select * From trending ORDER BY top DESC LIMIT 13")=== TRUE) 
    {

        if (mysqli_num_rows($sql) == 0)
        {
           for ($n=1;$n<=12;$n++)
           {
              echo'<div class="div_css_one">';
              echo'#</div>';
              echo'<div class="div_css_one_b">';
              echo'</div>';
            }
          }else{  

          while($data = mysqli_fetch_array($sql, MYSQLI_ASSOC)) 
          {
             echo '<li><a href="hashtag?hashtag='.$data['hashtag'].'"><p class="font-weight">#'.$data['hashtag'].'</p></a><spam class="trending_post_top">'.$data['top'].' posts</spam><li>';
           }
           //Cerrar recursos usados si fuere necesario
    }else{
           echo "Consulta errónea, revise la instrucción SQL: ".$sql
    }
}
    
answered by 13.09.2017 / 22:34
source
1

Your query is incorrect, it has a , more, at the end of the list of fields. This causes the function to return a FALSE . That FALSE is what you pass to the function mysqli_num_rows , which is what generates the second warning.

You can see more details of mysqli_query and mysqli_num_rows

    
answered by 13.09.2017 в 23:13