473,225 Members | 1,329 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,225 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 2171
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.