VicHost Posted September 6, 2010 Share Posted September 6, 2010 Hi folks, Complete No0b here when it comes to PHP and MySQL. I am in the middle of creating a PHP website. What I want to do is have the contents of a page in a MySQL table and have PHP gather the page content and display it on the page. Here is what I have in my home.tpl file: <?php $query = "SELECT home, FROM $database_name"; $result = mysql_query($query); ?> And, in my index.php I simply call that home.tpl to display the data by using: <?php include 'templates/default/home.tpl'; ?> Now, all I get is a blank page on index.php. Is there something else I should be doing? Remember, I am a complete No0b! Thanks folks. Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/ Share on other sites More sharing options...
objnoob Posted September 6, 2010 Share Posted September 6, 2010 Try <?php $query = "SELECT home FROM $database_name"; $result = mysql_query($query); $row = mysql_fetch_row($result); echo $row[0]; ?> You also wanna set $datebase_name to the TABLE name. You also need to create a MySQL Resource link using the mysql_connect() function. You should read up on using PHP to connect to MySQL and perform queries. Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107658 Share on other sites More sharing options...
coupe-r Posted September 6, 2010 Share Posted September 6, 2010 Try this, without the comma, since you only have 1 element. SELECT home FROM $database_name you can also do some testing on the same page with: $row = mysql_fetch_array($result); echo $row['home']; Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107659 Share on other sites More sharing options...
VicHost Posted September 6, 2010 Author Share Posted September 6, 2010 Cheers folks. Unfortunately, still having the same issue. The last one took away the footer from the page as well. Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107660 Share on other sites More sharing options...
coupe-r Posted September 6, 2010 Share Posted September 6, 2010 Can I ask why you are using a .tpl file? Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107662 Share on other sites More sharing options...
VicHost Posted September 6, 2010 Author Share Posted September 6, 2010 Can I ask why you are using a .tpl file? Sure mate. It's just that I want to create this site to be completely customisable via Template files. I want each page to be editable also via an admin section, where content will be updated using WYSIWYG. Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107663 Share on other sites More sharing options...
coupe-r Posted September 6, 2010 Share Posted September 6, 2010 I've never used .tml files, so I may not be any help. In your index.php file, do everything there, just to see if you can get anything to echo. That way we know if your getting results. We can then move to get the include to work. Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107665 Share on other sites More sharing options...
VicHost Posted September 6, 2010 Author Share Posted September 6, 2010 I've never used .tml files, so I may not be any help. In your index.php file, do everything there, just to see if you can get anything to echo. That way we know if your getting results. We can then move to get the include to work. Thanks mate. I have tried directly in index.php and same results. The Include is working fine. If I just type something into home.tpl such as "Test" it shows in index.php Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107669 Share on other sites More sharing options...
objnoob Posted September 6, 2010 Share Posted September 6, 2010 If this is all you have in home.tpl... It's not going to work. <?php $query = "SELECT home, FROM $database_name"; $result = mysql_query($query); ?> You at least need to define those variables in home.tpl or index.php, and echo the query results. I don't understand why you are including SQL statements from .tpl into an index.php. Keep it simple. Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107672 Share on other sites More sharing options...
VicHost Posted September 6, 2010 Author Share Posted September 6, 2010 If this is all you have in home.tpl... It's not going to work. <?php $query = "SELECT home, FROM $database_name"; $result = mysql_query($query); ?> You at least need to define those variables in home.tpl or index.php Thanks mate. So what do I need to add to this? Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107676 Share on other sites More sharing options...
coupe-r Posted September 6, 2010 Share Posted September 6, 2010 You are just quering the database and not grabbing any records. You need to grab the records with a mysql_fetch_array(); or any other fetch function. You can then echo them on the page. I don't see any echo statements or anywhere where you are trying to grab record data. Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107677 Share on other sites More sharing options...
objnoob Posted September 6, 2010 Share Posted September 6, 2010 Coupe-r has it correct. Look above to my original post.. There is the example you want. Again, there is there is no good reason to pursue in this manner. Storing HTML in a database can be a hassle, and restricts you from defining conditional HTML. Basically, a template should define a model of conditional and non conditional HTML, only querying data for non HTML insertion and to resolve any conditional HTML you may have in your template. Link to comment https://forums.phpfreaks.com/topic/212624-unable-to-retrieve-mysql-data/#findComment-1107682 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.