Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

search code not working

  • 12-02-2008 4:33pm
    #1
    Closed Accounts Posts: 51 ✭✭


    im trying to get a search engine working in my database, but it just isnt happening for me.
    Can someone please give me some advice on where to start?!
    thanks


Comments

  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    you're going to have to help us a bit before we can help you. what database? what language? what platform? what have you tried so far? what is it you are trying to achieve? The more information you give us the more we can help you.


  • Closed Accounts Posts: 51 ✭✭poissys


    Beano wrote: »
    you're going to have to help us a bit before we can help you. what database? what language? what platform? what have you tried so far? what is it you are trying to achieve? The more information you give us the more we can help you.
    im using php
    im using a my sql database, and im using the following code:

    i enter something into the textbox and then ht the search button, but it then reads: You are not authorized to view this page,with"http://localhost/<?=$PHP_SELF?>&quot; showing as the url address ,in the address bar
    iheres the code:

    <html>

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Search</title>
    </head>

    <body>
    <h2>Search</h2>
    <form name="search" method="post" action="<?=$PHP_SELF?>">
     <p align="center">Seach for:  <input type="text" name="find" />in
    <Select NAME="field" size="1">
    <Option VALUE="type">Tradesman</option>
    <Option VALUE="location">Location</option>
    </Select>
    <input type="hidden" name="searching" value="yes" />
    <input type="submit" name="search" value="Search" />
    </p>
    </form>
    </body>

    <?
    //This is only displayed if they have submitted the form
    if ($searching =="yes")
    {
    echo "<h2>Results</h2><p>";

    //If they did not enter a search term we give them an error
    if ($find == "")
    {
    echo "<p>You forgot to enter a search term";
    exit;
    }

    // Otherwise we connect to our Database
    mysql_connect("localhost", "root", "password") or die(mysql_error());
    mysql_select_db("database_name") or die(mysql_error());

    // We preform a bit of filtering
    $find = strtoupper($find);
    $find = strip_tags($find);
    $find = trim ($find);

    //Now we search for our search term, in the field the user specified
    $data = mysql_query("SELECT * FROM mytable WHERE upper($field) LIKE'%$find%'");

    //And we display the results
    while($result = mysql_fetch_array( $data ))
    {
    echo $result;
    echo " ";
    echo $result;
    echo "<br>";

    echo "<br>";
    echo "<br>";
    }

    //This counts the number or results - and if there wasn't any it gives them a little message explaining that
    $anymatches=mysql_num_rows($data);
    if ($anymatches == 0)
    {
    echo "Sorry, but we can not find an entry to match your query<br><br>";
    }

    //And we remind them what they searched for
    echo "<b>Searched For:</b> " .$find;
    }
    ?>
    </html>


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    [php]<?=$PHP_SELF;?>[/php] will not work in PHP5 by default.

    Try using this instead:

    [php]$_SERVER[/php]

    As well you have

    [php]if ($searching =="yes")[/php]

    Similarly this won't work as Global variables are turned off. Try using this instead:

    [php]if (isset($_POST)) {[/php]

    You also have variables such as $find that need to be done like
    [php]$find = $_POST;[/php]

    This may work in some PHP servers but you will have major compatibility problems moving so best to stick to the way i've mentioned above.


    Hope this helps.


  • Closed Accounts Posts: 51 ✭✭poissys


    thanks for that

    i made the changes to the self php issue and teh if yes, but did not know what u meant with the 'find' related issue

    i got the following error so far: http://localhost/$_SERVER
    any ideas?

    Webmonkey wrote: »
    [php]<?=$PHP_SELF;?>[/php] will not work in PHP5 by default.

    Try using this instead:

    [php]$_SERVER[/php]As well you have

    [php]if ($searching =="yes")[/php]Similarly this won't work as Global variables are turned off. Try using this instead:

    [php]if (isset($_POST)) {[/php]You also have variables such as $find that need to be done like
    [php]$find = $_POST;[/php]This may work in some PHP servers but you will have major compatibility problems moving so best to stick to the way i've mentioned above.


    Hope this helps.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    You must print it:

    [php]echo $_SERVER;[/php]


  • Advertisement
  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    poissys wrote: »
    im trying to get a search engine working in my database, but it just isnt happening for me.
    Can someone please give me some advice on where to start?!
    thanks

    If you want to start from scratch:

    http://en.wikipedia.org/wiki/Tf-idf

    I've written a perl seach engine based on TF-IDF. It has very high precision/recall and is very fast.

    Otherwise, just look on sourceforge.net and I'm sure there are a whole ream of MySQL search plugins. No point in re-inventing the wheel!


  • Closed Accounts Posts: 51 ✭✭poissys


    Cantab. wrote: »
    If you want to start from scratch:

    http://en.wikipedia.org/wiki/Tf-idf

    I've written a perl seach engine based on TF-IDF. It has very high precision/recall and is very fast.

    Otherwise, just look on sourceforge.net and I'm sure there are a whole ream of MySQL search plugins. No point in re-inventing the wheel!

    thanks for that, but are u sure thats teh right link u posted in the post?
    thanks


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    poissys wrote: »
    thanks for that, but are u sure thats teh right link u posted in the post?
    thanks

    Why don't you just use http://tsep.info/cms/ or something similar?


  • Closed Accounts Posts: 51 ✭✭poissys


    did u ever get a search working, for searching for db entries, where it would pull back that entire row?

    Cantab. wrote: »
    Why don't you just use http://tsep.info/cms/ or something similar?


  • Closed Accounts Posts: 51 ✭✭poissys


    thanks for the comments guys.
    i dont think iv made myself clear in terms of what i want:
    i need to get a search working, for searching for db entries, where it would pull back that entire row?
    for example, a textbox for make of car, and a textbox with model of car, then a search button, that will look specifically in the make and model columns, and return the entire rows, that match up with the search criteria.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Try

    [php]
    $model = $_POST;
    $field = $_POST;
    $sql_select = "SELECT * FROM cars WHERE match(cardesc, col2, col3....) against('$field' in boolean mode) AND model = '$model' order by carname ASC";

    $result = mysql_query($sql_select);
    [/php]



    You can then get the primary key as an identifier for the row, perhaps something like
    [php]
    while ($row = mysql_fetch_assoc($result))
    {
    $rowID = $row; //The name of your primary key in database
    $carname = $row;
    $cost = $row;

    //Now display a list of cars with their name and cost and make it go to a page to display info on the car
    printf("<a href=\"displaycar.php?carid=%d\"><strong>%s</strong> - € %1.2f</a><br />", $rowID,$carname,$cost);

    }
    [/php]

    You must do some research on your own but that will give you a bit of guidence i'm sure. I havn't tested the code obviously, sure errors there and you must change it to your own design but i'm sure you get the picture now I hope. Don't mind that printf thing, just print them out what ever way you want. Maybe using the echo or print functions.

    Hope this helps.


Advertisement