PHP on HTML Document?, Trying to display database data on HTML page. |
PHP on HTML Document?, Trying to display database data on HTML page. |
Jason70988 |
Mar 19 2010, 07:33 PM
Post
#1
|
Novice Group: Members Posts: 20 Joined: 11-March 10 Member No.: 11,346 |
Hi all, I've been trying to display information read from my MySQL database on a HTML heavy page.
What I've been trying to do (which I have read is possible) is similar to: CODE </html> <head></head> <body> <table><tr><td> <?php include "connect.php"; $getdata="SELECT name FROM products order by pid desc"; while($getdata3=mysql_fetch_array($getdata2)) { $getdata3[name]=strip_tags($getdata3[name]); print "<p><b>Name:</b> $getdata3[name]<br></p>"; } ?> </td></tr></table> </body> </html> Is this possible to do, or is there a better way to do this? Thank you for any help! Links: The layout I want the data displayed on. Content I want to display The problem I'm having |
Jason70988 |
Mar 19 2010, 08:58 PM
Post
#2
|
Novice Group: Members Posts: 20 Joined: 11-March 10 Member No.: 11,346 |
Ok I've just thought;
Would it be best to make it a PHP document and "print" all of the html code (each line individually) and just backslash all of the quotation marks? It just seems like a long-winded way of doing it. |
Brian Chandler |
Mar 19 2010, 11:49 PM
Post
#3
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
Hi all, I've been trying to display information read from my MySQL database on a HTML heavy page. What I've been trying to do (which I have read is possible) is similar to: CODE </html> <head></head> <body> <table><tr><td> <?php include "connect.php"; $getdata="SELECT name FROM products order by pid desc"; while($getdata3=mysql_fetch_array($getdata2)) { $getdata3[name]=strip_tags($getdata3[name]); print "<p><b>Name:</b> $getdata3[name]<br></p>"; } ?> </td></tr></table> </body> </html> Obviously it's possible to print info from a database; your bit of code has several problems. (Zerothly, it seems bizarre to put the whole content inside a one-celled table!) First, array subscripts in php should be quoted if strings. $getdata3['name'] etc Second, what does the _name_ field of the _products_ table contain? Why do you need to use strip_tags on it? (Who designed the database, where did it come from?) Third, why do all your variable names start with "getdata"? Fourth, have you read the mysql bits in the php manual? You can't call mysql_fetch_array() until you have a result from mysql_query(). So it's not likely that this will do what you want immediately, but then that's what debugging is for. Meanwhile, just look at the source of this page: QUOTE Here it is: CODE <Table width=970 border=0 align="center" cellpadding="10"> <tr> <td width=700> <?php print "<p><b>ID:</b> $getdata3[pid]<br></p>"; print "<p><b>Name:</b> $getdata3[name]<br></p>"; print "<p><b>Description:</b> $getdata3[description]<br></p>"; print "<p><b>Class:</b> $getdata3[class]<br></p>"; Clearly the php program is not being run. You need to fiddle with the .htaccess settings (or whatever) to get .html files passed through the php interpreter. Or you can rename the file as .php, and it may happen automatically, but fundamentally I think it's better for what is after all just an html page to be called something.html. HTH |
Jason70988 |
Mar 20 2010, 08:34 AM
Post
#4
|
Novice Group: Members Posts: 20 Joined: 11-March 10 Member No.: 11,346 |
QUOTE (Zerothly, it seems bizarre to put the whole content inside a one-celled table!) was just an example QUOTE First, array subscripts in php should be quoted if strings. $getdata3['name'] etc Thanks for tip, will amend. QUOTE Second, what does the _name_ field of the _products_ table contain? Why do you need to use strip_tags on it? (Who designed the database, where did it come from?) It is my database, on the server. I left the strip tags in when using the code, it's not just for that one field, but would work without i spose, thx QUOTE Third, why do all your variable names start with "getdata"? habit QUOTE Fourth, have you read the mysql bits in the php manual? You can't call mysql_fetch_array() until you have a result from mysql_query(). I left that out by accident, CODE $getdata2=mysql_query($getdata) or die("Could not get data"); QUOTE You need to fiddle with the .htaccess settings (or whatever) to get .html files passed through the php interpreter. I've tried this many times but always end up with this problem I've just tried adding print tags to every line, and backslashing all the quotation marks, but that didnt work either. Back to the drawing board |
Jason70988 |
Mar 21 2010, 09:49 AM
Post
#5
|
Novice Group: Members Posts: 20 Joined: 11-March 10 Member No.: 11,346 |
I've managed to start getting PHP errors on my HTML page, which is a step in the right direction I guess:
http://www.jasonsportfolio.co.uk/test.html |
Brian Chandler |
Mar 21 2010, 02:58 PM
Post
#6
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
404... but yes, once you get PHP errors, you only have to debug the program.
|
Jason70988 |
Mar 21 2010, 04:33 PM
Post
#7
|
Novice Group: Members Posts: 20 Joined: 11-March 10 Member No.: 11,346 |
|
Brian Chandler |
Mar 22 2010, 08:59 AM
Post
#8
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
404... but yes, once you get PHP errors, you only have to debug the program. Yea I missed a close php bracket. Managed to solve the issue by adding a php5 handler to the .html and .htm pages. Are there any security issues by doing this? Yes. If you are on a shared server (as I am, at pair.com) if you just let Apache run your php programs as itself (a user called something like 'nobody'), then you will need to leave the php files including the DB password readable by 'nobody', which in this case means anybody. So other users on the same server could in principle access the passwords. The answer is to use a wrapper program to call a copy of php in your own directory somewhere, so the php programs are run with user 'you'; then you can make the file including the password read-acess only to 'you'. You need to read your hosts documentation on this... |
Lo-Fi Version | Time is now: 29th March 2024 - 09:10 AM |