Is there any way to display the result of a query(mysqli) in the form of HTML?

Asked

Viewed 1,229 times

2

I have already searched intensively for a solution to show the result of a mysqli query inside an html one, but I can’t find anything. I saw that it is possible to present the result through one with the help of fetch_assoc() in php, but what I want doesn’t need to be shown inside a table from my perspective. In aixo is the code of what I want to show, already tested in full php and shows exactly what I want, the only obstacle at this time is to be able to incorporate this same result in an HTML page. I hope you can help me, regards.

     <?php
            require_once('connconf.php'); 

            $conn = mysqli_connect($server, $user, $pw, $bdname) or die ('Connection Error');

            $sqlquery = "select ID_Vote from Votes where ID_Player = '1'"; 

            if($result = mysqli_query($conn, $sqlquery))
                {

                    $rowcount = mysqli_num_rows($result);

                   echo $rowcount;   //this is the value i want to publish on a HTML <label>
                }

        ?>

4 answers

4

Complete HTML table

If you want to display all data from a table, regardless of the query, has this more elaborate solution, which shows the results in table format, with the field names including:

<?php
   require_once('connconf.php');
   $conn = mysqli_connect($server, $user, $pw, $bdname) or die ( 'Connection Error' );
   $sqlquery = "SELECT * FROM votes";

   $result = mysqli_query($conn, $sqlquery) or die ( mysqli_error( $conn ) );

   $header = true;
   echo '<table>';
   while( $res = mysqli_fetch_assoc( $result ) ) {
      if( $header ) {
         foreach( $res as $campo => $valor ) {
            echo'<th>'.htmlentities( $campo ).'</th>';
         }
         $header = false;
      }
      echo '<tr>';
      foreach( $res as $campo => $valor ) {
         echo'<td>'.htmlentities( $valor ).'</td>';
      }
      echo '</tr>';
   }
   echo '</table>';
?>

This solution adapts to the result of query independent of the number of columns, and already shows the names of the fields correctly in the table title.

Example:

SELECT * FROM cadastro

Upshot

 id nome documento  
1 Roberto 129.132.111-33
2 Maria 212.332.718-83
3 César 417.229.873-12
...  


Quick debug

If you’re just gonna make one debug fast, with little data that should suffice:

<?php
    require_once('connconf.php'); 
    $conn = mysqli_connect($server, $user, $pw, $bdname) or die ('Connection Error');
    $sqlquery = 'SELECT ID_Vote from Votes where ID_Player = 1'; 

    if( $result = mysqli_query($conn, $sqlquery) ) {
       echo '<pre>';
       print_r( mysqli_fetch_all( $result, MYSQLI_ASSOC ) );
       echo '</pre>';
    }
?>

0

Suppose you are making this query to be in the same file as you want to show the result in the label, let’s just call it for example by showing.php

php.

     <?php
            require_once('connconf.php'); 

            $conn = mysqli_connect($server, $user, $pw, $bdname) or die ('Connection Error');

            $sqlquery = "select ID_Vote from Votes where ID_Player = '1'"; 

            if($result = mysqli_query($conn, $sqlquery))
                {

                    $rowcount = mysqli_num_rows($result);
?>


                   <label><?php echo $rowcount;?></label>



<?php
                }

        ?>

The explanation is simple, you open and close the php "tag" whenever you want to use html.

0

I suppose everything is working properly in your Query, then to display the result of the variable $rowcount, on an html page, you can use this form within any file .php between appointments HTML:

<label><?=$rowcount?></label>
  • Yes the query itself is correct, the problem is to even incorporate the result of the query into the html page... I tested this line of code as you wrote and starting with '<? php' without forgetting to close '}' but still not showing.

0

Another possibility is to embed html in the following form.

Echo" <label>.$rowcount.</label>";
  • Rui, I tested it the way you said it, and what appeared to me on the html page was this: . $rowcount." ; } ? > Then I tested this way: Echo "<label>". $rowcount." </label>"; and this appeared to me: ". $rowcount.""; } ?>

Browser other questions tagged

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