Hi,
I’m new to MODx so the solution to this question may be laughably simple. However, I’ve looked around the forums and can’t seem to find the answer. Here goes :
I have a template variable (called [*test_tv*]) whose value I would like to use in a database query. (The TV contains the name of the table within the database that is relevant for the page using the template.) So there is a call to a chunk like this
{{db_call}}
which calls a snippet in this way :
[[test_db_call? &passed_table=’[*test_tv*]’ ]]
If the snippet is just this
<?php
echo $passed_table;
?>
then the correct table value is printed out.
But if the snippet looks like this
<?php
$dbhost = ’localhost’;
$dbuser = ’theuser’;
$dbpass = ’thepwd’;
$dbname = ’thedb’;
$dbtable = $passed_table;
echo $passed_table;
echo $dbtable;
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$dbrequest = ’SELECT * FROM ’.$dbtable;
echo $dbrequest;
$data = mysql_query($dbrequest) or die(mysql_error());
[... remaining code omitted ...]
then I get a SQL error arising from the query request being
"SELECT * FROM [*test_tv*]"
(as opposed to something like "SELECT * FROM ’testtable’" )
In other words, the specification of the TV is what is being used in the database request
rather than the value of the TV being passed.
Can anyone point me to something describing what I am doing wrong, or point the error out?
Thanks in advance for any assistance. And if I have overlooked something obvious or posted this in the
wrong forum, my apologies for that.
Regards,
Matt
PS Also, from searching the forums it seems there are more MODx-friendly ways of handling database
requests. I’m still working on trying to understand that ...
FWIW, if I change the line
$dbtable = $passed_table;
to
$dbtable = $modx->documentObject[’test_tv’][1];
then it works.
I also changed all the query code to its equivalent "$modx->db[...]" code. Certainly looks nicer...
Anyway, I’d still be interested to know how to directly use the TV if anyone knows.
Thanks for listening...
Matt
Thanks. And I appreciate the feedback.
Matt