0

I have a simple search bar problem thats driving me crazy!

I've followed a simple walk through and for the life of me cant get the thing running. https://owlcation.com/stem/Simple-search-PHP-MySQL

Im using XAMPP localHost, created the tutorial search database & articles table they requestd and still nothing...

But all I seem to get right now is... ...

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\searchBar\search.php:2 Stack trace: #0 {main} thrown in C:\xampp\htdocs\searchBar\search.php on line 2

...

with the search.php code looking like this-

...

<?php
    mysql_connect("localhost", "root", "") or die("Error connecting to database: ".mysql_error());
    /*
        localhost - it's location of the mysql server, usually localhost
        root - your username
        third is your password

        if connection fails it will stop loading the page and display an error
    */

    mysql_select_db("tutorial_search") or die(mysql_error());
    /* tutorial_search is the name of database we've created */
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
    $query = $_GET['query']; 
    // gets value sent over search form

    $min_length = 3;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM articles
            WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles is the name of our table

        // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

                echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }

        }
        else{ // if there is no matching rows do following
            echo "No results";
        }

    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
?>
</body>
</html>

...

if someone could help it would be appreciated as I feel a bit stupid right now.

Cheers, Dave.

dougalg
  • 1
  • 1
  • 2
    [Php manual](https://www.php.net/manual/en/function.mysql-connect.php) states that depending on php version mysql_connect() is either deprecated or removed. Looks like you are using php 7.0 or newer – Eugene Anisiutkin Feb 14 '20 at 13:21
  • You can not use that tutorial, it is quite outdated, as @EugeneAnisiutkin stated, the functions were remove some time ago. You need to use `mysqli` or `PDO` API to access mysql nowadays. – Frieder Feb 14 '20 at 13:24

1 Answers1

0

You seem to follow an old tutorial. According to https://www.php.net/manual/en/function.mysql-connect.php was removed starting from PHP version 7. You should use mysqli_connect.

Vadim Sirbu
  • 671
  • 4
  • 10
  • Hey, which one of the mysqli_connect codes do you suggest I use instead from that page? – dougalg Feb 14 '20 at 13:27
  • You can find an example here: https://www.php.net/manual/en/function.mysqli-connect.php; – Vadim Sirbu Feb 14 '20 at 13:31
  • Cheers that last link work. Now its giving me this error, any ideas? Success: A proper connection to MySQL was made! The my_db database is great. Host information: localhost via TCP/IP Fatal error: Uncaught Error: Call to undefined function mysql_real_escape_string() in C:\xampp\htdocs\searchBar\search.php:38 Stack trace: #0 {main} thrown in C:\xampp\htdocs\searchBar\search.php on line 38 – dougalg Feb 14 '20 at 13:47
  • You will have to switch from mysql to mysqli all over your code. For this particular case please check this link: https://www.php.net/manual/en/mysqli.real-escape-string.php – Vadim Sirbu Feb 14 '20 at 14:01
  • Hey, Thanks for the tips. After realising that walk through was a dud I did a bit of research and found another guide that has worked first time! https://code-boxx.com/php-mysql-search/ – dougalg Feb 14 '20 at 15:40