473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using variable names stored in database fields.

I'm working on a project where I need to store a CGI query in a
database field. The query contains variables which will be substitued
at runtime (e.g., today's date, key to select upon, etc), and may be
pointed to different URLs depending upon the table key.

The variable substitution works fine when hardcoding into the script,
e.g.:
$var1='data1';
$var2='data2';
$a="http://www.webserver.com/query.cgi?a=$var1&b=$var2";
$fd=fopen($a,"r");
...

However, the substitution fails when the query is pulled from a database
record, e.g.

//Only one record returned for testing purposes.
//
//location field (varchar) is:
//"http://www.webserver.com/query.cgi?a=$var1&b=$var2"

$var1='data1';
$var2='data2';
$dbconn=yadda yada;
$dbquery=pg_query($dbconn,"select key,location from schema.table");
$row=pg_fetch_assoc($dbquery);
$fd=fopen($row['location'],"r");
...

In this case, the variable substitution doesn't occur in the query (i.e.,
$var1 remains the string $var1, instead of changing into the value of say,
'03-23-2004'). I've tried different variations, and treating as a
variable variable (with $$row['url'] and ${$row['url']}. No joy.

Any ideas would be appreciated. Thanks.

-------------------------------------------------
Only in America will someone |
order a Big Mac, large fries, | bo**@visi.com
and a *Diet* Coke... |
Jul 17 '05 #1
4 2182
On Tue, 23 Mar 2004 11:33:16 -0600, bofh wrote:
I'm working on a project where I need to store a CGI query in a
database field. The query contains variables which will be substitued
at runtime (e.g., today's date, key to select upon, etc), and may be
pointed to different URLs depending upon the table key.

The variable substitution works fine when hardcoding into the script,
e.g.:
$var1='data1';
$var2='data2';
$a="http://www.webserver.com/query.cgi?a=$var1&b=$var2";
$fd=fopen($a,"r");
...

However, the substitution fails when the query is pulled from a database
record, e.g.

//Only one record returned for testing purposes.
//
//location field (varchar) is:
//"http://www.webserver.com/query.cgi?a=$var1&b=$var2"

$var1='data1';
$var2='data2';
$dbconn=yadda yada;
$dbquery=pg_query($dbconn,"select key,location from schema.table");
$row=pg_fetch_assoc($dbquery);
$fd=fopen($row['location'],"r");
...

In this case, the variable substitution doesn't occur in the query (i.e.,
$var1 remains the string $var1, instead of changing into the value of say,
'03-23-2004'). I've tried different variations, and treating as a
variable variable (with $$row['url'] and ${$row['url']}. No joy.

Any ideas would be appreciated. Thanks.

It's not working because the SQL result set is just a string that has no
relation whatsoever to the variables $var1 and $var2. I.e. the string is
not treated as PHP.

You could use eval().

Or you could use a regex replace. example:

preg_replace("/$var1/", $var1, $row['location']);
(...repeat for $var2...)

Or, if the query string is always going to include "a=$var1" and "b=$var2"
you could recreate the query string from scratch:

(assuming $row['location'] contains only the URL without the query
string):

$url = $row['location'] . "?a=$var1&b=$var2";

There are probably other ways to solve this problem, as there are always
at least two more ways to do something beyond even the half dozen you may
have thought of. good luck...

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #2
I kind of figured it was only being seen as a string, and I've thought of
a few ways around it (explode() and implode() being the most likely
candidates at present). I was just hoping that I had overlooked
something, and that some complexity could be engineered out.

On Tue, 23 Mar 2004, Jeffrey Silverman wrote:
On Tue, 23 Mar 2004 11:33:16 -0600, bofh wrote:
I'm working on a project where I need to store a CGI query in a
database field. The query contains variables which will be substitued
at runtime (e.g., today's date, key to select upon, etc), and may be
pointed to different URLs depending upon the table key.

The variable substitution works fine when hardcoding into the script,
e.g.:
$var1='data1';
$var2='data2';
$a="http://www.webserver.com/query.cgi?a=$var1&b=$var2";
$fd=fopen($a,"r");
...

However, the substitution fails when the query is pulled from a database
record, e.g.

//Only one record returned for testing purposes.
//
//location field (varchar) is:
//"http://www.webserver.com/query.cgi?a=$var1&b=$var2"

$var1='data1';
$var2='data2';
$dbconn=yadda yada;
$dbquery=pg_query($dbconn,"select key,location from schema.table");
$row=pg_fetch_assoc($dbquery);
$fd=fopen($row['location'],"r");
...

In this case, the variable substitution doesn't occur in the query (i.e.,
$var1 remains the string $var1, instead of changing into the value of say,
'03-23-2004'). I've tried different variations, and treating as a
variable variable (with $$row['url'] and ${$row['url']}. No joy.

Any ideas would be appreciated. Thanks.

It's not working because the SQL result set is just a string that has no
relation whatsoever to the variables $var1 and $var2. I.e. the string is
not treated as PHP.

You could use eval().

Or you could use a regex replace. example:

preg_replace("/$var1/", $var1, $row['location']);
(...repeat for $var2...)

Or, if the query string is always going to include "a=$var1" and "b=$var2"
you could recreate the query string from scratch:

(assuming $row['location'] contains only the URL without the query
string):

$url = $row['location'] . "?a=$var1&b=$var2";

There are probably other ways to solve this problem, as there are always
at least two more ways to do something beyond even the half dozen you may
have thought of. good luck...

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/


-------------------------------------------------
Only in America will someone |
order a Big Mac, large fries, | bo**@visi.com
and a *Diet* Coke... |
Jul 17 '05 #3
Tried out eval() on a lark, and that works just fine.

Thanks much.
On Tue, 23 Mar 2004, Jeffrey Silverman wrote:
On Tue, 23 Mar 2004 11:33:16 -0600, bofh wrote:
I'm working on a project where I need to store a CGI query in a
database field. The query contains variables which will be substitued
at runtime (e.g., today's date, key to select upon, etc), and may be
pointed to different URLs depending upon the table key.

The variable substitution works fine when hardcoding into the script,
e.g.:
$var1='data1';
$var2='data2';
$a="http://www.webserver.com/query.cgi?a=$var1&b=$var2";
$fd=fopen($a,"r");
...

However, the substitution fails when the query is pulled from a database
record, e.g.

//Only one record returned for testing purposes.
//
//location field (varchar) is:
//"http://www.webserver.com/query.cgi?a=$var1&b=$var2"

$var1='data1';
$var2='data2';
$dbconn=yadda yada;
$dbquery=pg_query($dbconn,"select key,location from schema.table");
$row=pg_fetch_assoc($dbquery);
$fd=fopen($row['location'],"r");
...

In this case, the variable substitution doesn't occur in the query (i.e.,
$var1 remains the string $var1, instead of changing into the value of say,
'03-23-2004'). I've tried different variations, and treating as a
variable variable (with $$row['url'] and ${$row['url']}. No joy.

Any ideas would be appreciated. Thanks.

It's not working because the SQL result set is just a string that has no
relation whatsoever to the variables $var1 and $var2. I.e. the string is
not treated as PHP.

You could use eval().

Or you could use a regex replace. example:

preg_replace("/$var1/", $var1, $row['location']);
(...repeat for $var2...)

Or, if the query string is always going to include "a=$var1" and "b=$var2"
you could recreate the query string from scratch:

(assuming $row['location'] contains only the URL without the query
string):

$url = $row['location'] . "?a=$var1&b=$var2";

There are probably other ways to solve this problem, as there are always
at least two more ways to do something beyond even the half dozen you may
have thought of. good luck...

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/


-------------------------------------------------
Only in America will someone |
order a Big Mac, large fries, | bo**@visi.com
and a *Diet* Coke... |
Jul 17 '05 #4
bo**@visi.com top-posted (corrected):
On Tue, 23 Mar 2004, Jeffrey Silverman wrote:
On Tue, 23 Mar 2004 11:33:16 -0600, bofh wrote:
> However, the substitution fails when the query is pulled from a database
> record, e.g. > In this case, the variable substitution doesn't occur in the query (i.e.,
> $var1 remains the string $var1, instead of changing into the value of say,
> '03-23-2004'). I've tried different variations, and treating as a
> variable variable (with $$row['url'] and ${$row['url']}. No joy.
It's not working because the SQL result set is just a string that has no
relation whatsoever to the variables $var1 and $var2. I.e. the string is
not treated as PHP.

You could use eval().
I kind of figured it was only being seen as a string, and I've thought of
a few ways around it (explode() and implode() being the most likely
candidates at present). I was just hoping that I had overlooked
something, and that some complexity could be engineered out.


As Jeffrey told you, eval() is your friend
http://www.php.net/eval
For example:

<?php
$data = 'URL?a=$var1&b=$var2';
$var1 = 'foobar';
$var2 = 'quux';

// you want to transform $data to 'URL?a=foobar&b=quux'
eval('$data = "' . $data . '";');
echo $data;
?>
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike Knight | last post by:
(I've also posted this problem on microsoft.public.excel.programming) I have a MS Access 2003 Database named "AS400 Fields.mdb". This database contains links to tables on an AS400. In MS...
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
6
by: rlrcstr | last post by:
The DBA team at the office controls the naming conventions for the database structure, but their naming convention are rather tedious. So typically I create a global module that I use as a mapping...
2
by: charliej2001 | last post by:
Hi all I'm trying to set up an Access database that will be able to import contact information from the lotus notes 6.5 Address book. The idea is that the code runs from access to import all of...
1
by: xcelmind | last post by:
Hello Dev. Guru, I want to at this time introduce myself. I am Stanley Ojadovwa by name. I’m a freelance and a newbie in web application development. I’m currently using ASP as my application...
10
by: Jaye | last post by:
Hi. I am a relative newbie to ASP and I am working on an application that uses ASP and an Oracle 9i database. I have a form that allows the user to query the database by selecting a client name(s)...
6
by: mike | last post by:
so I keep optimizing my fields down to the minimum character length necessary i.e., varchar(15), then I find out a month later its gotta get bigger, then a few months later, bigger again, etc. ...
1
by: Peter Herath | last post by:
I have created a report using sample codes taken by the forum. one problem is that the report displays the field/column names in the table in columnar format(field names display in rows.) but i want...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.