Need to substitute #where# with $where

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    #1

    Need to substitute #where# with $where

    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

  • noone

    #2
    Re: Need to substitute #where# with $where

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


    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);

    Comment

    • comp.lang.php

      #3
      Re: Need to substitute #where# with $where


      noone wrote:[color=blue]
      > comp.lang.php wrote:
      >[color=green]
      > > if ($willLimitByDB ) $sql = preg_replace('/#([^#]+)#/i', '$$1',
      > > $sql);[/color]
      >[color=green]
      > > This does not give me the results I want, instead of the value of
      > > $where in $sql, I literally get '$where' instead.[/color]
      >[color=green]
      > > How do I substitute #where# with $where?[/color]
      >[color=green]
      > > Thanx
      > > Phil[/color]
      >
      >
      > 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);[/color]

      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

      Comment

      • Marcin Dobrucki

        #4
        Re: Need to substitute #where# with $where

        comp.lang.php wrote:
        [color=blue]
        >
        > "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[/color]

        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

        Comment

        • comp.lang.php

          #5
          Re: Need to substitute #where# with $where


          Marcin Dobrucki wrote:[color=blue]
          > comp.lang.php wrote:
          >[color=green]
          > >
          > > "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[/color]
          >
          > 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[/color]

          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

          Comment

          • Marcin Dobrucki

            #6
            Re: Need to substitute #where# with $where

            comp.lang.php wrote:
            [color=blue][color=green][color=darkred]
            >>>"SELECT id, first_name, last_name, (SELECT count(id) #where#) as
            >>>paginate_tot al, email, address, city, state, zip, phone FROM person
            >>>$where "
            >>>
            >>>You want "#where#" inside the subselect substituted with $where[/color]
            >>
            >> 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?[/color][/color]
            [color=blue]
            > 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.[/color]

            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

            Comment

            Working...