473,804 Members | 3,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.c om/query.cgi?a=$va r1&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.c om/query.cgi?a=$va r1&b=$var2"

$var1='data1';
$var2='data2';
$dbconn=yadda yada;
$dbquery=pg_que ry($dbconn,"sel ect key,location from schema.table");
$row=pg_fetch_a ssoc($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 2213
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.c om/query.cgi?a=$va r1&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.c om/query.cgi?a=$va r1&b=$var2"

$var1='data1';
$var2='data2';
$dbconn=yadda yada;
$dbquery=pg_que ry($dbconn,"sel ect key,location from schema.table");
$row=pg_fetch_a ssoc($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=$va r2";

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.c om/query.cgi?a=$va r1&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.c om/query.cgi?a=$va r1&b=$var2"

$var1='data1';
$var2='data2';
$dbconn=yadda yada;
$dbquery=pg_que ry($dbconn,"sel ect key,location from schema.table");
$row=pg_fetch_a ssoc($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=$va r2";

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.c om/query.cgi?a=$va r1&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.c om/query.cgi?a=$va r1&b=$var2"

$var1='data1';
$var2='data2';
$dbconn=yadda yada;
$dbquery=pg_que ry($dbconn,"sel ect key,location from schema.table");
$row=pg_fetch_a ssoc($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=$va r2";

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
4202
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 Excel 2003, I have VBA code that creates and executes queries using the Access database, and returns the results to an Excel sheet. The first time the query is executed, results are returned to Excel in usually less than 10 seconds. However, if the...
0
2657
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 to call them from an ASP.Net page Every modern database system has a stored procedure language. SQL Server is no different and has a relatively sophisticated and easy to use system. This article will not attempt to go into depth in explaining...
6
1970
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 file to create constants with more appropriate field and table names and map them to the table and field names required by the DB team. But now, using a DataList, I'm having a problem. How can I use a variable name to indicate the field name in...
2
11848
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 the contacts, using COM. I've written the following vba code so far to import the contacts
1
3075
by: xcelmind | last post by:
Hello Dev. Guru, I want to at this time introduce myself. I am Stanley Ojadovwa by name. I知 a freelance and a newbie in web application development. I知 currently using ASP as my application server technology with Microsoft access as my database source. Just as I have introduced myself, I知 a newbie in web application development. I知 currently working on an application that will allow
10
2862
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) from a listbox and also set a time period for the search by selecting a start date and end date. Once the query is executed a form letter(s) is (are) generated to the client(s) that details the products that have been purchased during the time...
6
12912
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. Nowadays on sql server 2005 and on, how bad is it really to use varchar(max)? Is there really a big performance or storage hit or is it negligible? -Mike
1
2513
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 to be able to display filed names and its relevant data in tabular format in the repor but the code itself just displays only the fields in rows and another problem is that, to set the record source. the way i set the record source property of the...
0
10326
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project用lanning, coding, testing, and deployment謡ithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.