0
<?php

$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name="comment"; // Table name 


// connect to the mysql server
$link = mysql_connect($host, $username, $password)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
$db_found = mysql_select_db($db_name)
or die ("Could not select database because ".mysql_error());

     
      $result=mysql_query("select * from $tbl_name");

      while($row=mysql_fetch_assoc($result)){

 echo "ID: ".$row['id']."<br>";

 echo "name: ".$row['name']."<br>";
	 echo "comment: ".$row['comment']."<br>";
      }


?>

hello is there any way to insert the data into a table.
ive had a little go but it doesnt seem to like it

<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td> echo ".$row['id']." </td>
</tr>
</table>

i get a parse error

Edited by zeke188: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by colweb
0

This should display your data in a table:

$result=mysql_query("select * from $tbl_name");
if (!$result) {
	// no result from query, do not create empty table
}
else {
	// we have something to display, create table
	echo "<table width=\"400\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#FFFFFF\">";
        echo "<tr><td>ID</td><td>name</td><td>comment</td></tr>";
	while($row=mysql_fetch_assoc($result)){
		echo "<tr>";
		echo "<td>".$row['id']."</td>";
		echo "<td>".$row['name']."</td>";
		echo "<td>".$row['comment']."</td>";
		echo "</tr>";
	}
	echo "</table>";
}

If you want to use php code inside your html you schould write it like this:

<td><?php echo $row['id']; ?></td>

For using it in this way the file must have be recognized as a php file. For example "sometext.php" looks like a normal html file but will display the text "more text":

<html>
<head>
</head>
<body>
	<p>some text <?php echo "more text"; ?> and some more.</p>
</body>
</html>

Edited by colweb: Added some code

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.