2

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; 

 }

}
?>
Hertus
  • 147
  • 2
  • 11
  • Possible duplicate of [How to echo out table rows from the db (php)](http://stackoverflow.com/questions/2970936/how-to-echo-out-table-rows-from-the-db-php) **Or** [Display mysql data in formatted table](http://stackoverflow.com/questions/32236830/display-mysql-data-in-formatted-table) – FirstOne Dec 23 '15 at 23:15
  • Depends on the structure, you could generate the tables via the `while`-loop that fetches data from the database. None the less, `mysql_*` functions are deprecated since PHP 5.5 (and removed entirely in PHP 7) and you shoud [stop using them](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) if you can. – Qirel Dec 23 '15 at 23:30

3 Answers3

1

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

Community
  • 1
  • 1
mrroot5
  • 1,811
  • 3
  • 28
  • 33
  • Same as above. The mysql data doesn´t appear. If i do a normal query without table and styles it works. – Hertus Dec 24 '15 at 01:17
0

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>
Alaa M. Jaddou
  • 1,180
  • 1
  • 11
  • 30
0

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>
Webber Depor
  • 198
  • 4
  • 16