Reputation: 147
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;
}
}
?>
Upvotes: 2
Views: 1267
Reputation: 198
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>
Upvotes: 0
Reputation: 1961
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):
Upvotes: 1
Reputation: 1189
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>
Upvotes: 0