How to make a database search appear in a table?

Asked

Viewed 73 times

0

A few days ago, I’ve been trying to get my comic book search to be presented internally to a bordered table. However, although the server does not return any error, no results are shown. I have already made several changes in the syntax regarding the positioning of the Tables tags, putting it internally to WHILE but so far I have not had any success. It follows below the last way I tried:

<html>

<head>
    <title>Extraindo dados do BD</title>
</head>
<body>
    <?php
        mysql_connect("localhost","DB1","XXXX") or die(mysql_error());
        mysql_select_db("database1") or die(mysql_error());
        $selecaodedados= "SELECT * FROM meubancodedados";
        echo "<table style="1px">";
            echo    "<thead>    <tr> <th colspan='6'>Lista 1 </th></tr>"    
                ."</tread>";
            echo    "<tr>"
                ."<th> A dado </th> <th> B dado </th> <th> C dado </th>"
                ." <th>D dado </th> <th> E dado </th> <th> F total </th>"
                ."</tr>";
            echo    "</thead>";
            echo    " <tbody>";
        while($linha= mysql_fetch_array($selecaodedados)){
            echo "<tr>"
                ."<td>".$linha['A']. "</td><td> ". $linha['B']"
                . " </td><td> ".$linha['C']. "</td><td>  ". $linha['D']"
                . "</td><td>  ". $linha['E']."</td><td>  ".$linha['F']. "</td><br/>"
                ."</tr>";
            }
            echo "</tbody>";
            echo "</table>";            
        mysql_close();
    ?>
</body>

2 answers

0

To facilitate you can put one for within the while:

<table>
<?php
    while($linha= mysql_fetch_row($selecaodedados)){
        echo "<tr>";
        for($x=0; $x<count($linha); $x++)
            echo "<td>".$linha[$x]."</td>";
        echo "</tr>";
    }
?>
</table>

Use the mysql_fetch_assoc you will have a numerical vector for the information, so it will be easier to go through as a vector.

  • Grateful brother. In fact it became much more organized than just calling the data with the mysql_fetch_array. However there is no way to put the information in the head of the table or even stylize it by placing edge or similar? The website edition took away the part where I reported that I’m a beginner in programming, so I forgive you for any nonsense you’re saying.

  • @Lucasmenezes, it is still possible to do whatever you want with the table by CSS, just put the CSS code normally changing the table, to put the head also, you can use a echo with the table head, or put out the php tag.

  • I had tried, without success. The result I had was something strange. Both with the head inside php and outside, what the server returned to me was 39 repetitions of for(=0; written in line, and below the information from Head. So I asked if it was possible to style the table generated by the search. But I realized where the error of my syntax was. The "for" you recommended I use is missing a pair of keys circling it.

  • Just two more questions, brother. In case you don’t want to show all the BD columns, how would you recommend coding? And as the other friend reported that Mysql is falling into disuse, what you recommend as a good alternative?

  • mysql is not in disuse, the function mysql is becoming obsolete in php and are migrating to mysqli. In the case of fields, the best method would be not to select the fields in SQL, or to use the mysql_fetch_assoc and the foreach and a condition for the fields you do not wish to show

  • Although the warning of "avoid comments as Thanks" I find very clumsy and of a very great coldness, leave without thanks for the help. Grateful brother.

Show 1 more comment

0


Hello. I no longer recommend using Mysql, since it is obsolete and is discontinued. It has also been removed in version 7.0.0 of PHP.

But as the question was asked about Mysql, let’s answer it! In your code you failed to give query

The right thing would be:

<html>

<head>
    <title>Extraindo dados do BD</title>
</head>
<body>
    <?php
        mysql_connect("localhost","USUARIO","SENHA") or die(mysql_error());
        mysql_select_db("DATABASE") or die(mysql_error());
        $selecaodedados= "SELECT * FROM TABELA";
        $executar = mysql_query("$selecaodedados");
        echo "<table style='1px'>";
        echo    "<thead>    <tr> <th colspan='6'>Lista 1 </th></tr>";    
        echo "</tread>";
        echo   "<tr>";
        echo "<th> A dado </th> <th> B dado </th> <th> C dado </th>";
        echo " <th>D dado </th> <th> E dado </th> <th> F total </th>";
        echo "</tr>";
        echo  "</thead>";
        echo   " <tbody>";
    while($linha= mysql_fetch_array($executar)){
        echo "<tr>";
        echo "<td>".$linha['A']."</td><td>" .$linha['B'];
        echo  "</td><td>".$linha['C']."</td><td> ".$linha['D'];
        echo "</td><td>  ".$linha['E']."</td><td> ".$linha['F']."</td><br/>";
        echo "</tr>";
        }
        echo "</tbody>";
        echo "</table>";            
    mysql_close();
?>

Also, his code was missing many ";" As you can see, the changes were:

$execute = mysql_query("$selected");

while($line= mysql_fetch_array($run)){

  • Desde já eu não recomendo mais utilizar MySql, já que o mesmo é obsoleto e está descontinuado. E também foi removido na versão 7.0.0 do PHP. - mysql DBMS has been discontinued? Are you sure about this?

  • Yes, there was even a vote to discontinue: https://wiki.php.net/rfc/mysql_deprecation

  • Your mistake, the MYSQL DATABASE, remains firm, what was discontinued are the mysql_functions used within php.

  • But is that what I’m saying? Missing specify? OK, I specify!

  • Why did you answer the question in your answer?

  • I didn’t see it. Thank you!

  • Grateful for the help brother, but by mistake I left the query aside in the last example, although I had already tried, and even using it, the data is not shown.

  • As an alternative to Mysql what should I use?

  • After several tests I realized that what did not allow visualizing the BD information using the mysql_fetch_array in my case was a tag br forgotten from a previous cut. Very grateful for your help!

Show 4 more comments

Browser other questions tagged

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