Das246
asked on
eval() to run a dynamic sprintf() query
Hi,
I'm working on some code that I want to make portable across various pages within the site I'm working on. Most of the code is placed inside 'include' files and specific variables are set on each page to activate sections of the code. This is working fine except for a line of code that is supposed to set up a query via an eval() function. The code I have is as follows:
-------------------------- ---------- ---
<?php
// Code in page
$fillCheckBoxQry = "sprintf(\"SELECT unit_id FROM user_resident WHERE user_id = %s GROUP BY unit_id ORDER BY unit_id ASC\", $treeUser);";
// $treeUser is generated within the include files.
?>
-------------------------- ---------- ---
<?php
// Portion of code within include file
// $database_gatekeeper, $gatekeeper are database connection parameters
function fillCheckBox() {
// Write javascript function contents to fill checkboxes
global $database_gatekeeper, $gatekeeper, $fillCheckBoxQry, $treeUser;
mysql_select_db($database_ gatekeeper , $gatekeeper);
$query_rsCheckBox = eval($fillCheckBoxQry); // *** This is where the problem is!
$rsCheckBox = mysql_query($query_rsCheck Box, $gatekeeper) or die(mysql_error());
$row_rsCheckBox = mysql_fetch_assoc($rsCheck Box);
$totalRows_rsCheckBox = mysql_num_rows($rsCheckBox );
$strVars = "";
do {
$strVars .= $row_rsCheckBox['unit_id'] .", ";
} while ($row_rsCheckBox = mysql_fetch_assoc($rsCheck Box));
if ($strVars) {
echo " strVars = new Array(".substr($strVars,0, -2).")\n";
} else {
echo " strVars = new Array()\n";
}
mysql_free_result($rsCheck Box);
} // end fillCheckBox()
-------------------------- ---------- ---------
The above doesn't execute the sprintf() function. Can anyone point me in the right direction?
I'm working on some code that I want to make portable across various pages within the site I'm working on. Most of the code is placed inside 'include' files and specific variables are set on each page to activate sections of the code. This is working fine except for a line of code that is supposed to set up a query via an eval() function. The code I have is as follows:
--------------------------
<?php
// Code in page
$fillCheckBoxQry = "sprintf(\"SELECT unit_id FROM user_resident WHERE user_id = %s GROUP BY unit_id ORDER BY unit_id ASC\", $treeUser);";
// $treeUser is generated within the include files.
?>
--------------------------
<?php
// Portion of code within include file
// $database_gatekeeper, $gatekeeper are database connection parameters
function fillCheckBox() {
// Write javascript function contents to fill checkboxes
global $database_gatekeeper, $gatekeeper, $fillCheckBoxQry, $treeUser;
mysql_select_db($database_
$query_rsCheckBox = eval($fillCheckBoxQry); // *** This is where the problem is!
$rsCheckBox = mysql_query($query_rsCheck
$row_rsCheckBox = mysql_fetch_assoc($rsCheck
$totalRows_rsCheckBox = mysql_num_rows($rsCheckBox
$strVars = "";
do {
$strVars .= $row_rsCheckBox['unit_id']
} while ($row_rsCheckBox = mysql_fetch_assoc($rsCheck
if ($strVars) {
echo " strVars = new Array(".substr($strVars,0,
} else {
echo " strVars = new Array()\n";
}
mysql_free_result($rsCheck
} // end fillCheckBox()
--------------------------
The above doesn't execute the sprintf() function. Can anyone point me in the right direction?
ASKER
Sorry, still doesn't work.
$treeUser is another dynamic variable that is calculated while the page loads so if I perform the sprintf() too early there is no $treeUser var.
I really need to be able to store the dynamic 'SELECT' statement until all the variables are in place, then execute it.
thanks Das246
$treeUser is another dynamic variable that is calculated while the page loads so if I perform the sprintf() too early there is no $treeUser var.
I really need to be able to store the dynamic 'SELECT' statement until all the variables are in place, then execute it.
thanks Das246
ASKER
I got around the problem by creating a function before the include file...
function queryMaker($where) { // Create query for checkboxes
return "SELECT unit_id FROM user_resident WHERE user_id = '".$where."' GROUP BY unit_id ORDER BY unit_id ASC";
}
The include file then calls the function...
$query_rsCheckBox = queryMaker($treeUser);
If eval() can do the same I would still like to know.
cheers
Das246
function queryMaker($where) { // Create query for checkboxes
return "SELECT unit_id FROM user_resident WHERE user_id = '".$where."' GROUP BY unit_id ORDER BY unit_id ASC";
}
The include file then calls the function...
$query_rsCheckBox = queryMaker($treeUser);
If eval() can do the same I would still like to know.
cheers
Das246
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks loz,
That works great.
So does eval() have to be a complete line of code? Is there a way of combining an eval statement with a variable such as:
$var = eval($str_code);
You mentioned a return statement as another possible solution from within the eval()
cheers
Das246
That works great.
So does eval() have to be a complete line of code? Is there a way of combining an eval statement with a variable such as:
$var = eval($str_code);
You mentioned a return statement as another possible solution from within the eval()
cheers
Das246
if you wanted i suppose you could try $fillCheckBoxQry = "sprintf(\"SELECT unit_id FROM user_resident WHERE user_id = %s GROUP BY unit_id ORDER BY unit_id ASC\", $treeUser); return $fillCheckBoxQry;";
i think that'd work
loz
i think that'd work
loz
you're not actually executing the function, merely storing it as a string inside the variable.. you need this to have the function run:
$fillCheckBoxQry = sprintf("SELECT unit_id FROM user_resident WHERE user_id = '%s' GROUP BY unit_id ORDER BY unit_id ASC", $treeUser);
i've added quotes around the %s as well, since you'll need this if $treeUser is a string to be found in the database, otherwise MySQL will think it's a column name
loz