READ PHP - mysql_fetch_array

Asked

Viewed 41 times

1

Hello, I would like a help with the command below. I am trying a read in a database table but I cannot do the while, because it only shows the last record.

Note: Using the vardump she shows the loop normal.

<?
class AppNoticiasDestaque{

    function AppNoticiasDestaque() {
        $verQ = "SELECT * FROM noticias";

        $ver = mysql_query($verQ) or die(mysql_error());
        if(mysql_num_rows($ver) > 0) {

            while($verRow = mysql_fetch_array($ver)) {
            var_dump ($this->AppNoticiasDestaque = $verRow);
    }}
        @mysql_free_result($ver);
    }
}
?>

//resgatando o resultado
<?=$obj->AppNoticiasDestaque['titulo_noticia'] ?>
  • If possible, use mysql_fetch_assoc as it is faster than mysql_fetch_array

  • Okay, thank you very much. Changed to mysql_fetch_assoc. ;)

1 answer

0


Hi @David, hi @David, I’m not sure I understand what you want to do, but the problem I’ve noticed is in the following excerpt:

<?php $this->AppNoticiasDestaque = $verRow; ?>

For every time the code passes in the loop you are replacing everything you have saved with the next line, which results in only 1 record. You can resolve this by changing the return of your method to:

<?php $this->AppNoticiasDestaque[] = $verRow; ?>

For you to have an array with all the results, you can test using this:

<?
echo "<pre>";
print_r($obj->AppNoticiasDestaque);
?>

I think this will help you.

  • William, perfect! That was just the problem, I was almost giving up already. Kk Thank you very much, it really helped. Congratulations on your knowledge there, cheers.

Browser other questions tagged

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