Jump to content

Trying to retrive rows from db


Cardale

Recommended Posts

I am trying to figure out how I can retrieve rows of data and assign a value to each row and display them, but all the tutorials I have read don't really cover this.  Does anyone know of a good tutorial that covers getting data from mysql in particular and then using the data after it is retrieved.

 

I am using a templating system and want to use the data after I have it.  I feel I am being some what vauge, but don't know what I should be offering as far as my project goes.

Link to comment
https://forums.phpfreaks.com/topic/161610-trying-to-retrive-rows-from-db/
Share on other sites

I found an example I think would help better explain what I am trying to do.

 

// These are the smarty files
require 'libs/Smarty.class.php';

// This is a file which abstracts the DB connecting functionality (Check out PEAR)
require 'DB.php';

$smarty = new Smarty;

$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->use_sub_dirs = false;
$smarty->caching = true;

// This SQL statement will get the 5 most recently added new items from the database
$sql = 'SELECT * ';
$sql .= 'FROM `news` ';
$sql .= 'ORDER BY `id` DESC LIMIT 0, 5';

$result = mysql_query($sql) or die("Query failed : " . mysql_error());

// For each result that we got from the Database
while ($line = mysql_fetch_assoc($result))
{
$value[] = $line;
}

// Assign this array to smarty...
$smarty->assign('news', $value);

// Display the news page through the news template
$smarty->display('news.tpl');

// Thanks to David C James for a code improvement 

 

In this example it has a news.tpl file and unfortunately would need multiple tpl files and all these assignments for each one ..what I am wondering is how I can have one index file and one tpl file instead and not have it just looped...which basically forces you to break up your work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.