By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,760 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,760 IT Pros & Developers. It's quick & easy.

Need to substitute #where# with $where

P: n/a
if ($willLimitByDB) $sql = preg_replace('/#([^#]+)#/i', '$$1',
$sql);

This does not give me the results I want, instead of the value of
$where in $sql, I literally get '$where' instead.

How do I substitute #where# with $where?

Thanx
Phil

Mar 15 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
comp.lang.php wrote:
if ($willLimitByDB) $sql = preg_replace('/#([^#]+)#/i', '$$1',
$sql); This does not give me the results I want, instead of the value of
$where in $sql, I literally get '$where' instead. How do I substitute #where# with $where? Thanx
Phil

given: $sql="select * from table";
and $where="where a = 'a'";

$sql .= $where;
//concatenate it before you edit it...
if ($willLimitByDB) $sql = preg_replace('/#([^#]+)#/i', '$$1', $sql);

Mar 15 '06 #2

P: n/a

noone wrote:
comp.lang.php wrote:
if ($willLimitByDB) $sql = preg_replace('/#([^#]+)#/i', '$$1',
$sql);

This does not give me the results I want, instead of the value of
$where in $sql, I literally get '$where' instead.

How do I substitute #where# with $where?

Thanx
Phil

given: $sql="select * from table";
and $where="where a = 'a'";

$sql .= $where;
//concatenate it before you edit it...
if ($willLimitByDB) $sql = preg_replace('/#([^#]+)#/i', '$$1', $sql);


Thanx but you were looking at the wrong "WHERE.."

the query is this

"SELECT id, first_name, last_name, (SELECT count(id) #where#) as
paginate_total, email, address, city, state, zip, phone FROM person
$where "

You want "#where#" inside the subselect substituted with $where

I got it though thanx to someone that knows RegExp modifiers

$sql = preg_replace('/#([^#]+)#/ie', '$$1', $sql);

Phil

Mar 15 '06 #3

P: n/a
comp.lang.php wrote:

"SELECT id, first_name, last_name, (SELECT count(id) #where#) as
paginate_total, email, address, city, state, zip, phone FROM person
$where "

You want "#where#" inside the subselect substituted with $where


So basically you are selecting "id, first_name, last_name ..." and
count of the returned results? Why not just ask the DB how many rows it
returned?

/marcin
Mar 16 '06 #4

P: n/a

Marcin Dobrucki wrote:
comp.lang.php wrote:

"SELECT id, first_name, last_name, (SELECT count(id) #where#) as
paginate_total, email, address, city, state, zip, phone FROM person
$where "

You want "#where#" inside the subselect substituted with $where


So basically you are selecting "id, first_name, last_name ..." and
count of the returned results? Why not just ask the DB how many rows it
returned?

/marcin


Because the resultset will have, for example, only 20 rows while the
entire table might have 20,000 rows. I must show how many total
records exist, all the while, only display a unit of 20.

Phil

Mar 16 '06 #5

P: n/a
comp.lang.php wrote:
"SELECT id, first_name, last_name, (SELECT count(id) #where#) as
paginate_total, email, address, city, state, zip, phone FROM person
$where "

You want "#where#" inside the subselect substituted with $where
So basically you are selecting "id, first_name, last_name ..." and
count of the returned results? Why not just ask the DB how many rows it
returned?

Because the resultset will have, for example, only 20 rows while the
entire table might have 20,000 rows. I must show how many total
records exist, all the while, only display a unit of 20.


But your original post said:
"How do I substitute #where# with $where?"

So if your count-select is also governed by the same "where"
statement, then your count will be that of the selected set.

There are also simpler ways (IMHO) to get the total, eg, just run a
second separate select query with only count(id).

/m
Mar 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.