transform Mysql record into php variable and show in HTML?

Asked

Viewed 1,001 times

1

I am asking to search in the database a result equal to a variable url that I already have, it looks right, but I want to pick another column in the same row of the url that it was looking for like this:

| registration | name |

search by matricule, when finding, takes the corresponding name and puts the result as the value of a variable. And shows me in HTML. Help me please :(

This is an HTML page:

     <?php

        include ('conecta.php');

        $connection = mysqli_connect($host, $user, $pass,$database);
         if (!$connection) {
         echo ("Servidor temporariamente fora de servi?o"); 
        }

        $query = "SELECT * FROM resultado"
        . "WHERE nu_mat LIKE ".$url."";

        $result =  mysqli_query($connection,$query);
        if (!$result) {
          die("Query invalida na selecao dos dados");
        }

        while ($dados =  mysqli_fetch_array($result,MYSQLI_ASSOC)) {

           $dados = ['nm_aluno'];
        }


        ?>
<BODY>
<?php 
         echo"<p class='texto'>"; echo"$dados"; echo"</p>";
         ?>
</BODY>
  • after in html I ask to show and does not show me anything

  • 1

    html does not seem to be in question.

  • <?php &#xA; echo"<p class='texto'>"; echo"$dados"; echo"</p>";&#xA; ?>

  • edited the question

1 answer

3


The code $dados = ['nm_aluno']; may be invalid if you are using a version earlier than 5.4 php and will not do what you are imagining.

An assignment(creates) is made from an array to $dados with the value nm_aluno, Lembresse that to access an array you must specify the variable($dados) and which key is desired(nm_aluno) square-bracketed.

That instruction must stay inside the while to be executed N times

while ($dados =  mysqli_fetch_array($result,MYSQLI_ASSOC)) {
   echo "<p class='texto'>". $dados['nm_aluno'] ."</p>";
}

When you have questions about how to use an array you can use the functions print_r() or var_dump() to get fucked up ;)

  • vlw!

  • @rray worked!

  • @rray thank you!

  • 1

    @Bacco I edited with the suggestion.

  • but then you have another answer?

  • @Sarah how so? before php5.4 you can only create arrays like this $var = array('nm_aluno', 'matricula'); 5.4 p front can be used $var = ['nm_aluno', 'matricula'];

  • @Sarah the answer is using the correct syntax. What we are saying is that your code has a syntax that PHP 5.4 up would accept, but anyway, even accepting the syntax, would not suit your need. What you wrote would serve for other kind of thing completely different than what you want.

  • then how it would be for my need?

  • vc was creating a new array every while turn so $dados = ['nm_aluno']; the syntax of what you want is $dados['nm_aluno'] no sign of equal :P

  • ta I understood kkk

  • personal thank you

Show 6 more comments

Browser other questions tagged

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