PDA

View Full Version : How to set a variable for a cached template in a custom page?


bzcomputers
09-14-2013, 07:04 PM
I'm trying to use data from MYSQL to cache a template.

I know everything with the MYSQL database connection works fine, but I can't seem to get the syntax right for placing the variable within the globaltemplates array.


What I'm working with:


// pre-cache templates used by all actions
$globaltemplates = array('cruisecams_ports', 'cruisecams_ports_menu', 'adblock_cruisecams_sidebar', 'sociallinks');



I'm trying to replace 'cruisecams_ports' with the data from MYSQL stored in $portcamtemplate.


Currently I'm just getting this error "Fatal error: Call to undefined method dbPDO::query_first_slave()" anytime I try and place the variable in the array.

All other MYSQL data works fine in the rendered page it's just when I try to set a variable in the template array that the issue occurs.

ozzy47
09-14-2013, 08:45 PM
What does the code look like that you are running the query?

bzcomputers
09-14-2013, 08:55 PM
// Get parameters from url query string
$db = new dbPDO();

$cruiseportcam_id = !empty($_GET['id'])?intval($_GET['id']):1;


// get all the values form your table limited to the first matching row (there should only be one)
$query = "SELECT * FROM cruiseportcams WHERE cruiseportcam_id = $portcamid";


// the prepare and bind methods prevent any sort of SQL injection
$stmt = $db->prepare($query);
$stmt->bindValue("$portcamid",$cruiseportcam_id);
$stmt->execute();

// we are only expecting 1 row here...
$portcam = $stmt->fetch(PDO::FETCH_ASSOC);
// now $portcam is an array of the values from your database


$portcamtemplate = $portcam['cruiseportcam_template'];

kh99
09-14-2013, 10:20 PM
I don't know what dbPDO is, but you probably need to include files to define it.

bzcomputers
09-14-2013, 11:29 PM
I don't know what dbPDO is, but you probably need to include files to define it.
http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/

PDO is a newer and more secure way for PHP to access databases.

It is working fine pulling db data I just want to place a template name from the database into the cached templates array but I'm not sure how to write the correct reference to it in PHP to make it work.

I've tried options like:
1) $portcamtemplate
2) $portcam['cruiseportcam_template']
3) echo $portcamtemplate


..and about a dozen other variations

kh99
09-15-2013, 01:46 AM
Oh, yeah OK. For some reason I was thinking your error said "Call to undefined method dbPDO" but I guess I spaced out and only read part of it.

Maybe the array keys are numbers and you could use the number of the column with the template name, like $portcam[3]. (I assume the column name is 'cruiseportcam_template' and you tried $portcam['cruiseportcam_template'] already).

But I'm not sure if what you're doing makes sense. I think the only thing caching templates does is save a database read for each one by getting them all at the beginning, so if you have to do a read just to get the template name you wouldn't save anything.

bzcomputers
09-15-2013, 02:42 AM
Oh, yeah OK. For some reason I was thinking your error said "Call to undefined method dbPDO" but I guess I spaced out and only read part of it.

Maybe the array keys are numbers and you could use the number of the column with the template name, like $portcam[3]. (I assume the column name is 'cruiseportcam_template' and you tried $portcam['cruiseportcam_template'] already).

But I'm not sure if what you're doing makes sense. I think the only thing caching templates does is save a database read for each one by getting them all at the beginning, so if you have to do a read just to get the template name you wouldn't save anything.

You're right, I guess I'll just skip the caching of it. Not really much being saved anyway.


Interesting though that actually both $portcamtemplate and $portcam['cruiseportcam_template'] work just fine when referring to it when it needs to be rendered but for some reason both cause that error when trying to cache the template.


Thanks.