简体   繁体   中英

mysql print data to display to html

i've got a basic php script that's connects to mysql and print data but i'm having issues making it print via html the html i have atm this is only part of the html only the important part. Sorry i have to type lots of crap so you can see this code because stackoverflow is such an amazing website, It helps lots and lots of people

<?php
$servername = "localhost";
$username = "root";
$password = "toor";
$dbname = "a3wasteland";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT UID, BattlEyeGUID, CreationDate, Name, BankMoney FROM PlayerInfo";
$result = $conn->query($sql);

?>
<div class='cWidgetContainer ipsHide' data-controller='core.front.widgets.area' data-role='widgetReceiver' data-orientation='horizontal' data-widgetArea='header'>
    <ul class='ipsList_reset'>

    </ul>
  </div>


<div id="elCmsPageWrap" data-pageid="2">

<div>
  <div class='ipsGrid ipsGrid_collapsePhone'>
    <div class='ipsGrid_span6'>



  <div class='cWidgetContainer ipsHide' data-controller='core.front.widgets.area' data-role='widgetReceiver' data-orientation='horizontal' data-widgetArea='col1'>
    <ul class='ipsList_reset'>

    </ul>
  </div>

    </div>
    <div class='ipsGrid_span6'>



  <div class='cWidgetContainer ipsHide' data-controller='core.front.widgets.area' data-role='widgetReceiver' data-orientation='horizontal' data-widgetArea='col2'>
    <ul class='ipsList_reset'>

    </ul>
  </div>

    </div>
  </div>
</div>
</div>



  <div class='cWidgetContainer ' data-controller='core.front.widgets.area' data-role='widgetReceiver' data-orientation='horizontal' data-widgetArea='footer'>
    <ul class='ipsList_reset'>


          <li class='ipsWidget ipsWidget_horizontal ipsBox' data-blockID='plugin_20_sodPhpWidget_paxe9xcu5' data-blockConfig="true" data-blockTitle="PHP Code" data-controller='core.front.widgets.block'>

<div class='ipsWidget_inner '>


    <p class='ipsType_reset ipsType_medium ipsType_light'>  
  <style>
  .specialType_center th {
    text-align: center; 
  }
  </style>
</style>
  <h2 class="ipsType_sectionTitle ipsType_reset cForumTitle ipsResponsive_hideTablet ipsResponsive_hidePhone <center> ">Banned Users</h2></center>
    <table class="ipsTable ipsTable_responsive ipsTable_zebra ipsBox ipsType_center specialType_center ipsResponsive_hideTablet ipsResponsive_hidePhone">
      <thead> 
   <tr>
      <th>UID</th>
      <th>BattlEyeGUID</th>
      <th>CreationDate</th>
      <th>Name</th>
      <th>BankMoney</th>
    </tr>
</thead>
<tbody>
<tr>
<?php
/* Other code */
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
?>
        <td><span class="ipsType_negative"><?=$row["UID"]?></span></td></tr>
        <td><span class="ipsType_negative"><?=$row["BattlEyeGUID"]?></span></td>
        <td><span class="ipsType_negative"><?=$row["CreationDate"]?></span></td>
        <td><span class="ipsType_negative"><?=$row["Name"]?></span></td>
        <td><span class="ipsType_negative"><?=$row["BankMoney"]?></span></td>
<?php } 
} else { ?>
    <td colspan="5">0 results</td>
<?php } ?>
</tr>
</tbody>
    </table>
  <p class='ipsType_right ipsType_light ipsType_small ipsResponsive_hideTablet ipsResponsive_hidePhone' style='margin-right: 10px;'>Last Update: 15.08.2016, 10:41 </p>
</p>

</div></li>

</ul>
  </div>

You can print your data inside the html like:

Your While code block:

<?php
// use <tr><td></td></tr> inside the loop.
while($row = $result->fetch_assoc()) {
?>
    <tr>
        <td><span class="ipsType_negative"><?=$row["PlayerUID"]?></span></td>
        <td><span class="ipsType_negative"><?=$row["PlayerKills"]?></span></td>
        <td><span class="ipsType_negative"><?=$row["AIKILLs"]?></span></td>
        <td><span class="ipsType_negative">TeamKills</span></td>
        <td><span class="ipsType_negative">DeathCount</span></td>
    </tr>
<?php
}
?>

Try this:

<thead> 
   <tr>
      <th>PlayerUID</th>
      <th>PlayerKills</th>
      <th>AIKILLs</th>
      <th>TeamKills</th>
      <th>DeathCount</th>
    </tr>
</thead>
<tbody>

<?php
/* Other code */
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
?>
 <tr>     <!-- start tr within the loop -->
   <td><?php echo $row["PlayerUID"]; ?></td>
   <td><?php echo $row["PlayerKills"]; ?></td>
   <td><?php echo $row["AIKILLs"]; ?></td>
   <td> <!-- Other data --></td>
   <td><!-- Other data --></td>
</tr>     <!-- end tr within the loop -->
<?php } 
} else { ?>
    <tr><td colspan="5">0 results</td></tr> 
<?php } ?>
</tbody>

The finishing code

<?php
$servername = "localhost";
$username = "root";
$password = "toor";
$dbname = "a3wasteland";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT UID, BattlEyeGUID, CreationDate, Name, BankMoney FROM PlayerInfo";
$result = $conn->query($sql);

?>
<div class='cWidgetContainer ipsHide' data-controller='core.front.widgets.area' data-role='widgetReceiver' data-orientation='horizontal' data-widgetArea='header'>
    <ul class='ipsList_reset'>

    </ul>
  </div>


<div id="elCmsPageWrap" data-pageid="2">

<div>
  <div class='ipsGrid ipsGrid_collapsePhone'>
    <div class='ipsGrid_span6'>



  <div class='cWidgetContainer ipsHide' data-controller='core.front.widgets.area' data-role='widgetReceiver' data-orientation='horizontal' data-widgetArea='col1'>
    <ul class='ipsList_reset'>

    </ul>
  </div>

    </div>
    <div class='ipsGrid_span6'>



  <div class='cWidgetContainer ipsHide' data-controller='core.front.widgets.area' data-role='widgetReceiver' data-orientation='horizontal' data-widgetArea='col2'>
    <ul class='ipsList_reset'>

    </ul>
  </div>

    </div>
  </div>
</div>
</div>



  <div class='cWidgetContainer ' data-controller='core.front.widgets.area' data-role='widgetReceiver' data-orientation='horizontal' data-widgetArea='footer'>
    <ul class='ipsList_reset'>


          <li class='ipsWidget ipsWidget_horizontal ipsBox' data-blockID='plugin_20_sodPhpWidget_paxe9xcu5' data-blockConfig="true" data-blockTitle="PHP Code" data-controller='core.front.widgets.block'>

<div class='ipsWidget_inner '>


    <p class='ipsType_reset ipsType_medium ipsType_light'>  
  <style>
  .specialType_center th {
    text-align: center; 
  }
  </style>
</style>
  <h2 class="ipsType_sectionTitle ipsType_reset cForumTitle ipsResponsive_hideTablet ipsResponsive_hidePhone <center> ">Arma 3 Player Stats</h2></center>
    <table class="ipsTable ipsTable_responsive ipsTable_zebra ipsBox ipsType_center specialType_center ipsResponsive_hideTablet ipsResponsive_hidePhone">
      <thead> 


   <tr>
      <th>BattlEyeGUID</th>
      <th>CreationDate</th>
      <th>Name</th>
      <th>BankMoney</th>
      <th>UID</th>
    </tr>
</thead>
<tbody>
<tr>
<?php
/* Other code */
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
?>
        <td><span class="ipsType_negative"><?=$row["UID"]?></span></td></tr>
        <td><span class="ipsType_negative"><?=$row["BattlEyeGUID"]?></span></td>
        <td><span class="ipsType_negative"><?=$row["CreationDate"]?></span></td>
        <td><span class="ipsType_negative"><?=$row["Name"]?></span></td>
        <td><span class="ipsType_negative"><?=$row["BankMoney"]?></span></td>
<?php } 
} else { ?>
    <td colspan="5">0 results</td>
<?php } ?>
</tr>
</tbody>
    </table>
  <p class='ipsType_right ipsType_light ipsType_small ipsResponsive_hideTablet ipsResponsive_hidePhone' style='margin-right: 10px;'>Last Update: 15.08.2016, 10:41 </p>
</p>

</div></li>

</ul>
  </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM