Mysql returns only the first record

Asked

Viewed 1,394 times

1

I created this function, but, it returns only the first record of the table.

function pega_conteudo_pela_id($id_assunto){

    global $conexao;

    $query3 = "SELECT * FROM `{$id_assunto}`";

    $result3 = mysqli_query($conexao,$query3) or die("MySQL error: " . mysqli_error($conexao) . "<hr>\nQuery: $query3");  

    while ($leiDB = mysqli_fetch_array($result3)) {
        return utf8_encode($leiDB['conteudo']. "<br/>");
    }
}

But if I put it on the links page, it works and takes all the records

    case 'codigo-civil':

        $query3 = "SELECT * FROM `codigo-civil`";

        $result3 = mysqli_query($conexao,$query3);

        while ($leiDB = mysqli_fetch_array($result3)) {
            echo utf8_encode($leiDB['conteudo']. "<br/>");
        }

        break;

2 answers

2


You’re using return within the function pega_conteudo_pela_id, but in practice it seems to me that this function does not need to return anything because in the other example you are doing echo what you need. The return aborts the function. Removes the return and puts echo, or if you want the function to return the contents of this query you can do so:

echo pega_conteudo_pela_id($variavel);

and within the function:

$results = '';
while ($leiDB = mysqli_fetch_array($result3)) {
    $results.= utf8_encode($leiDB['conteudo']. "<br/>");
}
return $results;

A semantic note: when you call the function of pega_conteudo_pela_id gives me the idea that you want to fetch data through a BD column ID. But in your example the parameter $id_assunto of the function is used to select a column. I would change these names not to cheat on the functionality they meet.

  • That’s right, I switched to echo and it worked, didn’t know it, the return back only one result. Thanks!!!

  • @I made an Dit in case you really want to use the return of function.

  • thanks, there is some difference between using the echo and the return, besides the return stop execution in the first loop?

  • 1

    @Allergy has to do with data flow and how you want to use the function return. It is practical to use function return along the line that called the function for code organization reasons. If you call a function that is in another file and the function does HTML echo it can be difficult to know what the code is doing. But basically there’s no effect on the code other than the return interrupt the function.

1

The "Return" command interrupts the execution loop in the first loop, so it is only returning the first record it finds.

Change return for echo and see the difference

return utf8_encode($leiDB['conteudo']. "<br/>");

Exchange for echo

echo utf8_encode($leiDB['conteudo']. "<br/>");

Obs: I’ll stick to commenting on the code, good programming practices, etc. But I find it valid to guide you that the code is very poorly written. Look for good programming practices.

  • kkkkkkkk, very badly written? kkkk, was even kind, vlw. Have some glaring mistake to advise me? but anyway, vlw by the help

  • rsrs.. is that it is not the case to extend the subject to another theme.. I think you can study and, when you have questions about the subject, ask that the guys will help you..

  • @Three points I can mention are the use of global, vulnerability to SQL Injection and the use of functions mysql_*

  • Thanks @gmsantos, I will try to improve these points, despite being very curious, to know what the problem of these three points and how to solve them, kkkk. Hugging

  • @These three links explain the problems of each point. If you have a question that is not covered in these questions, you can open a new question :)

  • OK @gmsantos, I will study them, thank you very much for your help, you have given at least one direction. Vlw man. Hugging

Show 1 more comment

Browser other questions tagged

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