New to PHP - Search Question

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

    #1

    New to PHP - Search Question

    Help..

    Just learning php/mysql from a book and have put together a test page
    as below.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>
    <title>Sample Page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
    />
    </head>

    <body>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    <label>Please enter Town:
    <input type="text" name="town" /></label>
    <input type="submit" value="GO" />
    </form>

    <p>Search Results for: <?php echo $_GET['town']; ?></p>

    <?php

    $connection = mysql_connect(' localhost', '????????', '????????');
    $db = mysql_select_db ("test", $connection);
    $result = @mysql_query ('SELECT name FROM site01_details WHERE town
    LIKE Bury');
    while ($row = mysql_fetch_arr ay($result))
    { echo '<p>' . $row['name'] . '</p>'; }

    ?>


    </body>

    </html>

    This is published at www.freeweekends.co.uk

    The database has fields for name (name of a business) and town (town in
    which the business is in).
    I firstly wanted to create a list for the businesses in the town 'Bury'
    but this fails.

    Secondly I wanted to pass the town search string entered in the form to
    replace 'Bury'.

    Sorry but I am totally new to this and just taking my first steps, so
    the fault is probably going to be an obvious one.

    P.s. the search is OK if I look for all names or towns so the database
    connection and naming is correct.

    Many thanks

    Alec

  • Wolfgang Forstmeier

    #2
    Re: New to PHP - Search Question

    This seems that there is nothing in the Database ?!
    try other mysql_query like this:

    $sql = "Select name from site01_details where town like '%Bury%'";
    $result = mysql_query($sq l) or die(mysql_error (mysql_errno));

    Hope this helps ..
    Wolfgang ...

    Alec wrote:[color=blue]
    > Help..
    >
    > Just learning php/mysql from a book and have put together a test page
    > as below.
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml-strict.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml">
    >
    > <head>
    > <title>Sample Page</title>
    > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
    > />
    > </head>
    >
    > <body>
    >
    > <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
    > <label>Please enter Town:
    > <input type="text" name="town" /></label>
    > <input type="submit" value="GO" />
    > </form>
    >
    > <p>Search Results for: <?php echo $_GET['town']; ?></p>
    >
    > <?php
    >
    > $connection = mysql_connect(' localhost', '????????', '????????');
    > $db = mysql_select_db ("test", $connection);
    > $result = @mysql_query ('SELECT name FROM site01_details WHERE town
    > LIKE Bury');
    > while ($row = mysql_fetch_arr ay($result))
    > { echo '<p>' . $row['name'] . '</p>'; }
    >
    > ?>
    >
    >
    > </body>
    >
    > </html>
    >
    > This is published at www.freeweekends.co.uk
    >
    > The database has fields for name (name of a business) and town (town in
    > which the business is in).
    > I firstly wanted to create a list for the businesses in the town 'Bury'
    > but this fails.
    >
    > Secondly I wanted to pass the town search string entered in the form to
    > replace 'Bury'.
    >
    > Sorry but I am totally new to this and just taking my first steps, so
    > the fault is probably going to be an obvious one.
    >
    > P.s. the search is OK if I look for all names or towns so the database
    > connection and naming is correct.
    >
    > Many thanks
    >
    > Alec
    >[/color]

    Comment

    • Alec

      #3
      Re: New to PHP - Search Question

      UPDATE

      Stupid me.....

      Problem solved with ('SELECT name FROM site01_details WHERE town
      LIKE "Bury"');

      But question still applies about using the entered form text in the
      search string.

      Many thanks.

      Alec

      Comment

      • ranii4u@gmail.com

        #4
        Re: New to PHP - Search Question

        Here,

        First of all in ur PHP code, U have to confirm that u get $_GET['town']
        != "" ,if it is not NULL then u have make below changes in ur syntax
        as:

        $result = @mysql_query "SELECT name FROM site01_details WHERE town
        LIKE %"'.$_GET['town'].' ");


        RANI.

        Comment

        • Alec

          #5
          Re: New to PHP - Search Question

          Rani

          Will try that now. Thanks.

          Wolfgang, sorry I meant under www.freeweekends.co.uk/test.php

          Thanks both

          Alec

          Comment

          • JDS

            #6
            Re: New to PHP - Search Question

            On Tue, 30 Aug 2005 02:45:54 -0700, Alec wrote:
            [color=blue]
            > Problem solved with ('SELECT name FROM site01_details WHERE town
            > LIKE "Bury"');[/color]

            Your SQL statement should:

            * use single quotes around the value after LIKE
            http://dev.mysql.com/doc/mysql/en/string-syntax.html

            * There really is no reason to use LIKE if you do not use wildcards -- the
            percent sign "%". Use equals "=" instead

            Your SQL statement should look like:

            $sql_statement = "SELECT name FROM site01_details WHERE town = 'Bury'";

            You are best off putting the SQL statment in a variable and putting the
            variable into the mysql_query() function:

            $result = mysql_query($sq l_statement);

            (I don't know why some books don't do this straight away, or at all. It
            doesn't make sense to me to teach something one way and then un-teach it
            later.)

            I have other comments on coding style and whatnot but this is enough for
            now, I imagine.

            COWS

            --
            JDS | jeffrey@example .invalid
            | http://www.newtnotes.com
            DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

            Comment

            Working...