Jump to content

Unable to Retrieve MySQL Data


VicHost

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.