Jump to content

Displaying A Php Table On An Existing Html Page


Box Face
 Share

Recommended Posts

Hey there.I'm quite new to PHP and web programming, but I'm managing to grasp the general concept so far. I've gone through the tutorials on the main website, and I've successfully created a form that allows users to write in a "guest book" (using MySQL) for my site. The form works, and it stores data in the MySQL database, which is what I want. The only problem I have is that I can't actually display the current contents of the gestbook on a website built in HTML, instead I can do it on a page just using PHP. My current code that I'm using is as follows:

<?phpmysql_connect("SERVER","USERNAME","PASSWORD");mysql_select_db("DATABASENAME");$result = mysql_query("SELECT * FROM guestbook");while($row = mysql_fetch_array($result))  {  echo $row['Name'] . " " . $row['Comment'];  echo "<br />";  }mysql_close($con);?>

All I'm doing at the moment is putting that in the right place on my website, I feel that this is probably wrong somewhere, because when I view the website it just displays a chunk of the code, as opposed to what I want.Can anyone point me in the right direction? Maybe I'm missing an initial line to tell the browser that we're looking at a piece of PHP code.Also, I'm going to point out that I know that the code doesn't actually view the table, but I'm just getting the PHP piece working, then I can work on it viewing the table! ;DMany thanks,Box Face

Link to comment
Share on other sites

Hey there.I'm quite new to PHP and web programming, but I'm managing to grasp the general concept so far. I've gone through the tutorials on the main website, and I've successfully created a form that allows users to write in a "guest book" (using MySQL) for my site. The form works, and it stores data in the MySQL database, which is what I want. The only problem I have is that I can't actually display the current contents of the gestbook on a website built in HTML, instead I can do it on a page just using PHP. My current code that I'm using is as follows:
<?phpmysql_connect("SERVER","USERNAME","PASSWORD");mysql_select_db("DATABASENAME");$result = mysql_query("SELECT * FROM guestbook");while($row = mysql_fetch_array($result))  {  echo $row['Name'] . " " . $row['Comment'];  echo "<br />";  }mysql_close($con);?>

All I'm doing at the moment is putting that in the right place on my website, I feel that this is probably wrong somewhere, because when I view the website it just displays a chunk of the code, as opposed to what I want.Can anyone point me in the right direction? Maybe I'm missing an initial line to tell the browser that we're looking at a piece of PHP code.Also, I'm going to point out that I know that the code doesn't actually view the table, but I'm just getting the PHP piece working, then I can work on it viewing the table! ;DMany thanks,Box Face

Hi, I think what everyone is trying to say is that if you are creating a dynamic page where things will change due to user input and if you are using php which works server side then the page needs to be a php extension. PHP works in harmony with html.I imagine you will have to look at your code again and see how you can change it to make it work for you in the way you need.You should be able to create a table in your php page and give each cell a variable name. Then you can use these to manipulate them.Good luck with the project.RegardsJustin
Link to comment
Share on other sites

Guest Wishmaster

You may use the below code. It is in simplest form and i hope it will help you. But as all have said before please use php extension such as you may name this file FETCH.PHP :) <html><head></head><body><?php$myconn=mysql_connect("hostname", "username", "password") ; mysql_select_db ("database name" ,$myconn) ;$query = "select*from tablename";$result = mysql_query($query);?><Div align="center"><table border="1" width="95%" cellpadding="0"><tr><th width="25%">Name</th><th width="25%">E-mail</th><th width="50%">Message</th></tr><?phpwhile($query_data = mysql_fetch_array($result)) {$name=$query_data["name"];$email=$query_data["email"];$message= $query_data["message"];echo"<tr>\n";echo"<td width=\"25%\" align=\"center\"> $name </td>\n";echo"<td width=\"25%\" align=\"center\"> $email </td>\n";echo"<td width=\"25%\" align=\"center\"> $message </td>\n";echo"</tr>\n";}?></table></div></body></html>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...