Problem When printing an SQL query on the PHP page

Asked

Viewed 70 times

1

I have an Sql query and my code is only printed the first line of the query, Does anyone have any idea what might be my problem ?

ERROR: Warning: mysql_fetch_assoc() expects Parameter 1 to be Resource,

<?php 
    include ("conexao.php");
    //$query = "SELECT SUM(valor), pagador from conta GROUP by pagador"
    $consulta = "SELECT SUM(valor) as soma, pagador from conta GROUP by pagador";
    $result = mysqli_query($conexao,$consulta) or die(mysql_error());
    $linha = mysqli_fetch_array($result);
    $total = mysqli_num_rows($result);

    // se o número de resultados for maior que zero, mostra os dados
    if($total > 0) {
        //inicia o loop que vai mostrar todos os dados
        do {
?>
<p><?=$linha['soma']?> / <?=$linha['pagador'];?></p>
<?php
        // finaliza o loop que vai mostrar os dados
        }while($linha = mysql_fetch_assoc($result));
        // fim do if 
     }
    mysql_close($conexao);
?>
  • or die(mysql_error()) does not work with Mysqli, so it is or die(mysqli_error($conexao)); the same to mysql_fetch_assoc

1 answer

2


See if it helps dear friend:

<?php 
    include ("conexao.php");
    $consulta = "SELECT SUM(valor) as soma, pagador from conta GROUP by pagador";
    $result = mysqli_query($conexao,$consulta) or die(mysqli_error($conexao));

    while($linha = mysqli_fetch_array($result,MYSQLI_ASSOC))
    {
        echo "{$linha['soma']} / {$linha['pagador']}</p>";
    }

    mysqli_close($conexao);
?>

I don’t know what’s in the.php related file, so I can’t tell you about it, but I believe that the code there should be something close to:

<?php $conexao=mysqli_connect("servidor","usuario","senha","banco"); ?>

Browser other questions tagged

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