Can anyone help, why doesn't this work ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    Can anyone help, why doesn't this work ??

    I have a from with 2 fields:
    Company & Name

    Depening which is completed, one of the following queries will be run:

    if($Company){
    $query = "Select C*
    From tblsample
    Where ID = $Company
    Order By Company ASC";
    }

    if($Name){
    $query = "SELECT *
    FROM TBLsample
    WHERE Contact = "%$Name%"
    ORDER BY Contact ASC";
    }


    $result = mysql_query($qu ery);
    $number = mysql_numrows($ result);
    for ($i=0; $i<$number; $i++) {

    $CompanyName = mysql_result($r esult,$i,"Compa ny");
    $ContactName = mysql_result($r esult,$i,"Conta ct");

    Print "Company: $CompanyName";
    Print "<P>Name: $ContactName";
    }

    This work fine for 'Company', but not for 'Name'

    How do I do a wild search using $Name in the Where statement.. ?

    Ie $Name = Jim
    So %Jim% should result in :
    Jim
    jimmy
    jimm
    etc !!

    Thanks
  • Daniel Tryba

    #2
    Re: Can anyone help, why doesn't this work ??

    James <James@nothere. com> wrote:[color=blue]
    > $q="SELECT ... Contact = "%$Name%" ORDER BY Contact ASC";[/color]
    ^begin string ^endstring
    ^beginstring ^endstring

    Learn the differences between " and ' and the use of these within a
    string (http://nl.php.net/manual/en/language.types.string.php).

    Other problems are realted to sql.

    -wildcards (%) only work on strings AFAIK
    -strings need to be quoted (eg ').

    --

    Daniel Tryba

    Comment

    • Andy Hassall

      #3
      Re: Can anyone help, why doesn't this work ??

      On Sat, 28 Jun 2003 12:22:02 +0000 (UTC), Daniel Tryba
      <news_comp.lang .php@canopus.nl > wrote:
      [color=blue]
      >James <James@nothere. com> wrote:[color=green]
      >> $q="SELECT ... Contact = "%$Name%" ORDER BY Contact ASC";[/color]
      > ^begin string ^endstring
      > ^beginstring ^endstring
      >
      >Learn the differences between " and ' and the use of these within a
      >string (http://nl.php.net/manual/en/language.types.string.php).
      >
      >Other problems are realted to sql.
      >
      >-wildcards (%) only work on strings AFAIK
      >-strings need to be quoted (eg ').[/color]

      And wildcards in SQL only apply if you use 'LIKE' instead of '='.

      --
      Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
      Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

      Comment

      • James

        #4
        Re: Can anyone help, why doesn't this work ??

        My form and results are on one page.
        If I use :

        if ($Company) {
        $query = "Select Company, Contact
        From tblworking
        Where ID = $Company
        Order By Company ASC";
        }

        if ($Name ) {
        $query = "SELECT *
        FROM TBLWorking
        WHERE Contact Like '%$Name%'
        ORDER BY Contact";
        }


        $result = mysql_query($qu ery);
        $number = mysql_numrows($ result);
        for ($i=0; $i<$number; $i++) {

        $CompanyName = mysql_result($r esult,$i,"Compa ny");
        $ContactName = mysql_result($r esult,$i,"Conta ct");

        Print "Company: $CompanyName";
        Print "<P>Name: $ContactName";
        }

        As soon as I enter the page it searchs using $Name, even though I have not
        entered any information.

        This results in me getting a complete list printed on screen, with out me
        having to search.

        How do I get this to show just the form, then the form and the results ?

        Thanks



        On Sat, 28 Jun 2003 15:38:53 +0100, James <James@NotHere. com> wrote:
        [color=blue]
        >Thanks, its now working using:
        >
        >WHERE Contact Like '%$Name%'
        >
        >Cheers
        >
        >On Sat, 28 Jun 2003 13:36:19 +0100, Andy Hassall <andy@andyh.co. uk> wrote:
        >[color=green]
        >>On Sat, 28 Jun 2003 12:22:02 +0000 (UTC), Daniel Tryba
        >><news_comp.la ng.php@canopus. nl> wrote:
        >>[color=darkred]
        >>>James <James@nothere. com> wrote:
        >>>> $q="SELECT ... Contact = "%$Name%" ORDER BY Contact ASC";
        >>> ^begin string ^endstring
        >>> ^beginstring ^endstring
        >>>
        >>>Learn the differences between " and ' and the use of these within a
        >>>string (http://nl.php.net/manual/en/language.types.string.php).
        >>>
        >>>Other problems are realted to sql.
        >>>
        >>>-wildcards (%) only work on strings AFAIK
        >>>-strings need to be quoted (eg ').[/color]
        >>
        >> And wildcards in SQL only apply if you use 'LIKE' instead of '='.[/color][/color]

        Comment

        • Roger

          #5
          Re: Can anyone help, why doesn't this work ??


          "James" <James@NotHere. com> wrote in message
          news:dh1rfvkjst ndhgsmdl16g2njh getj71r8v@4ax.c om...[color=blue]
          > I have a from with 2 fields:
          > Company & Name
          >
          > Depening which is completed, one of the following queries will be run:
          >
          > if($Company){
          > $query = "Select C*
          > From tblsample
          > Where ID = $Company
          > Order By Company ASC";
          > }
          >
          > if($Name){
          > $query = "SELECT *
          > FROM TBLsample
          > WHERE Contact = "%$Name%"
          > ORDER BY Contact ASC";
          > }
          >
          >
          > $result = mysql_query($qu ery);
          > $number = mysql_numrows($ result);
          > for ($i=0; $i<$number; $i++) {
          >
          > $CompanyName = mysql_result($r esult,$i,"Compa ny");
          > $ContactName = mysql_result($r esult,$i,"Conta ct");
          >
          > Print "Company: $CompanyName";
          > Print "<P>Name: $ContactName";
          > }
          >
          > This work fine for 'Company', but not for 'Name'
          >
          > How do I do a wild search using $Name in the Where statement.. ?
          >
          > Ie $Name = Jim
          > So %Jim% should result in :if($Name){
          > Jim
          > jimmy
          > jimm
          > etc !!
          >
          > Thanks[/color]

          if ( $Company )
          {
          $query = "Select C* From tblsample Where ID LIKE '%$Company%' Order By
          Company ASC";
          }
          if ( $Name )
          {
          $query = "SELECT * FROM TBLsample WHERE Contact LIKE '%$Name%' ORDER BY
          Contact ASC";
          }


          Comment

          Working...