dcsimg
www.webdeveloper.com
Results 1 to 2 of 2

Thread: mysql php search engine

  1. #1
    Join Date
    May 2017
    Posts
    1

    mysql php search engine

    Hi so I'm trying to do a search engine where they sear the site for stuff and when they sear it comes up with a link to a page on my website so its like an inhouse search engine.... I have just ran the mysql connect and db connect and it is all connected fine but when i try to grab data from the search engine it just comes up with This page isnít working

    (website name) is currently unable to handle this request.
    HTTP ERROR 500......

    this is on my index page..

    <div class="searchbar">
    <form action="search.php" method="get">
    <input type="text" name="query" placeholder="Search....." size="20"/>
    <input type="submit" value="Search"/>
    </form>
    </div>

    and this is the search.php.
    <div class="searchbar">
    <form action="search.php" method="get">
    <input type="text" name="k" size="20" value="<?php echo $_GET['k']; ?>"/>
    <input type="submit" value="Search"/>
    </form>
    </div>

    <?php

    $query = ($_GET['query']);

    $db_host = "localhost";
    $db_username = "pubadmin21";
    $db_password = "00000";
    $db_name = "pubdata21";

    @mysql_connect ("$db_host","$db_username","$db_password") or die ("could not connect to mysql");
    @mysql_select_db ("$db_name") or die ("No database");

    $query = mysql_query("SELECT * FROM search WHERE title = '.$search.'");
    if(mysql_num_rows($query) =>1 {

    while($a = mysql_fetch_array($query)) {
    echo "<a href='".$a['link']."'>".$a['title']."</a><p>".$a['description']."</p>";

    } else {

    echo "No results found!";
    }
    ?>

  2. #2
    Join Date
    May 2016
    Location
    Southern California
    Posts
    80
    First, you shouldn't be using mysql_connect, that function is deprecated! Look into using PDO instead and use prepared statements.

    Your $search variable within your sql is not declared. I see you're assigning $query twice, once to $_GET, and after that, to your msql_query.

    Also this ($_GET['k']) on your search.php form is going to throw an error and pretty much halt your script, unless you're passing it every time to the page or you're checking for it with isset(). I don't see an input with name=k on your index.php only on your search.php page.

    HTML Code:
    <!-- will error out -->
    <input type="text" name="k" size="20" value="<?php echo $_GET['k']; ?>"/>
    HTML Code:
    // instead do 
    <?php
    $term = isset($_GET['k']) ? trim($_GET['k']) : '';
    ?>
    <input type="text" name="k" size="20" value="<?php echo $term; ?>"/>
    To help you in debugging your website add this to the top of your php, remove or set to false once your code is in production / live:

    PHP Code:
    ini_set('display_errors'true);
    error_reporting(E_ALL); 
    PDO tutorial is available here, please make the switch or risk losing your data:
    https://phpdelusions.net/pdo#prepared

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles