error in mysql_result()

Asked

Viewed 135 times

4

I am again looking for help to understand an error given in a call to mysql_result(), which I cannot solve this error:

Warning: mysql_result() expects Parameter 1 to be Resource, Object Given in C: wamp www\

I don’t know what parameter you ask for.

That is the code:

 <?php 
          $visitas_total = mysqli_query($conexao,"SELECT Sum(visitas) AS visitas FROM lp_post")
                        or die(mysql_error());
       if(@mysqli_num_rows($visitas_total) <= '0') echo '';
       $views = 0;
       $visitas = mysql_result($visitas_total, $views, 'Visitas') ;

         ?>

someone could help me?

  • 1

    please arrange the post.

  • Sorry dear for my mistake. Thank you.

  • Try to take out the third parameter of the mysql_result and say what was the return

  • Sorry Rafael Acioly, I deleted all comments without that goal. But I tell you that in the way in which you guided me, you eliminated the error, but you did not print for me the numbers but the word Array, can guide me how to do?

  • except the third 'visits' error persists

1 answer

5


Use mysqli_fetch_assoc() or _array() to get the query return, do not mix themysql_ API with the new mysqli.

$sql = "SELECT Sum(visitas) AS visitas FROM lp_post";
$visitas_total = mysqli_query($conexao,$sql) or die(mysqli_error($conexao));
if(mysqli_num_rows($visitas_total) <= 0){
    echo 'Nenhum resultado foi econtrado';
}else{
    $visitas = mysqli_fetch_assoc($visitas_total);
    echo $visitas['visitas']; 
} 

If your query returns more than one line, use a while to iterate all the results.

while($row = mysqli_fetch_assoc($visitas_total)){
    echo $row['visitas'] .'<br>';
}

Recommended reading:

Why do they say using @arroba to suppress errors is a bad practice?

  • @Saulo vc need to print the right array key, it will always be the(s) name(s) of the database fields, so it will be, echo $visitas['visitas'];

  • Thank you so much that with affection can help me, everything worked out. Thank you.

  • @Aulo, if the answer solved the problem, you can mark it as accepted, How and why to accept an answer?, also see the tour to better understand how the site works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.