I just posted this in General Help, and then realized it probably should have been here... Sorry.
I currently have the following code in a program I'm working on:
$table_name = "articles";
$r_fields = "SELECT id, title, body, imagedesc, teaser, imagedesc,";
$sql = "$r_fields \n"
.boolean_sql_select(
boolean_inclusive_atoms($q),
$fulltext_key)." as relevance \n"
."FROM $table_name \n"
."WHERE \n"
.boolean_sql_where($q,$fulltext_key)." \n"
."HAVING relevance>0 \n"
."ORDER BY relevance DESC \n"
."$limit \n";
The above code (along with some other include files and variables) works as expected, generating a MySQL query. However, what I really need is for $table_name and $r_fields to be dynamic.
So, I worte the following two functions:
function selectedfields($rfields) {
$nofields = count($rfields);
IF ($nofields == "1") {
foreach ($rfields as $value) {
echo "SELECT $value"; }
}
ELSE {
print "SELECT ";
foreach ($rfields as $value) {
echo "$value, "; }
}
}
function tablename($tables) {
$nofields = count($tables);
IF ($nofields == "1") {
foreach ($tables as $value) {
echo "$value"; }
}
ELSE {
foreach ($tables as $value) {
echo "$value, "; }
}
}
These functions work as expected. The top one (selectedfields($rfields)) generates output identical to $r_fields. And the bottom one (tablename($tables)) generates output identical to $table_name.
So... how do I work these functions into the $sql statement? I've tried calling the functions directly within the $sql definition, but it's not working. For example, if I replace the line:
$sql = "$r_fields \n"
With:
$sql = selectedfields($rfields)." \n"
Then I get an error in my SQL syntax, eventhough the output of $sql looks right. What gives? Any ideas?