简体   繁体   中英

Php echo in table and with css

I have the following code:

<div id="Container">
<div class="tester">
<table>
<tr><th class="links"><span style="color: #ffd100">MYSQL DATA</span></th></tr>
<tr><th class="links">MYSQL DATA</th></tr>
<tr><th class="links">MYSQL DATA</th></tr>
<tr><th class="links">MYSQL DATA</th></tr>
</table>
</div>

I want the variables "MYSQL DATA" out of my mysql with php echo. I have the conncetion to the database but i don´t know where to insert the echo $row->name; etc.

<?php 

    mysql_connect("localhost", "XXXXX","XXXXXX") or die ("Verbindung nicht möglich");
    mysql_select_db("XXXXXX") or die ("Datenbank existiert nicht");

  $abfrage = "SELECT `name` FROM `TEST`"; 
  $ergebnis = mysql_query($abfrage); 
  while($row = mysql_fetch_object($ergebnis)) 
    { 

    echo $row->name; 
    echo $row->id; 

 }

}
?>

Example with mysqli and echo th with php:

<div id="Container">
    <div class="tester">
        <table>
<?php 
$link = mysqli_connect("localhost", "XXXXX","XXXXXX") or die ("Verbindung nicht möglich");
mysqli_select_db($link, "XXXXXX") or die ("Datenbank existiert nicht");

$abfrage = "SELECT `name` FROM `TEST`"; 
$ergebnis = mysqli_query($link, $abfrage);
$i=0; 
while($row = mysqli_fetch_object($ergebnis)) 
{
    if ($i === 0) { ?>
<tr>
    <th class="links">
        <span style="color: #ffd100"><?php echo $row->name; ?></span>
    </th>
</tr>
<?php } else { ?>
<tr>
    <td class="links"><?php echo $row->name; ?></td>
</tr>
<?php
    }
    $i++;
}
?>
        </table>
    </div>
</div>

Documentation (english and german):

mysqli connect (en)

mysqli select db (de)

mysqli query (de)

mysqli fetch object

UPDATED

<div id="Container">
<div class="tester">
<table>
<tr>
<th>
name
</th>
<?php 
mysql_connect("localhost", "XXXXX","XXXXXX") or die ("Verbindung nicht möglich");
mysql_select_db("XXXXXX") or die ("Datenbank existiert nicht");

$abfrage = "SELECT `name` FROM `TEST`"; 
$ergebnis = mysql_query($abfrage); 
while($row = mysql_fetch_object($ergebnis)) 
{ ?>
    <tr>
        <td class="links"><?php echo $row->name; ?></td>
    </tr>
<?php }
?>
</table>
</div>
</div>

You should save this as .php not .html

<div id="Container">
<div class="tester">
<table>
<?php 

    mysql_connect("localhost", "XXXXX","XXXXXX") or die ("Verbindung nicht möglich");
    mysql_select_db("XXXXXX") or die ("Datenbank existiert nicht");

  $abfrage = "SELECT `name` FROM `TEST`"; 
  $ergebnis = mysql_query($abfrage); 
  while($row = mysql_fetch_array($ergebnis)){ 
   if($row[0])
      echo '<tr><th class="links"><span style="color: #ffd100">'.$row->name.'</span></span></th></tr>';
   else
       echo '<tr><th class="links">'.$row->name.'</span></th></tr>';
  }

?>
</table>
</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