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; 

 }

}
?>
2

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Same as above. The mysql data doesn´t appear. If i do a normal query without table and styles it works.
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>

8 Comments

This creates all rows as table header cells. (I know the op's code is like that, but it's worth mentioning ^^)
just change the th to td and add row before the while with table headers that you want
i edit it to make them td :) with th before thanks for mention FirstOne
Thanks for this code. I tried it out, but the mysql data doesn´t appear for some reason.
Nothing, just the <th>name</th> and the div container.
|
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.