0

Hi I'm getting this error when making a search on my website.

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /home/u321547826/public_html/search.php:14 Stack trace: #0 {main} thrown in /home/u321547826/public_html/search.php on line 14

I'm working with this code, can anyone help?

<?php

$button = $_GET['submit'];
$search = $_GET['search'];

if (!$button)
    echo "you didn't submit a keyword";
else {
    if (strlen($search) <= 1)
        echo "Search term too short";
    else {
        echo "You searched for <b>$search</b> <hr size='1'></br>";
        mysql_connect("", "", "");
        mysql_select_db("");

        $search_exploded = explode(" ", $search);

        foreach ($search_exploded as $search_each) {
            $x++;
            if ($x == 1)
                $construct .= "keywords LIKE '%$search_each%'";
            else
                $construct .= "AND keywords LIKE '%$search_each%'";

        }

        $construct = "SELECT * FROM SEARCH_ENGINE WHERE $construct";
        $run       = mysql_query($construct);

        $foundnum = mysql_num_rows($run);

        if ($foundnum == 0)
            echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1. 
    Try more general words. for example: If you want to search 'how to create a website'
    then use general keyword like 'create' 'website'</br>2. Try different words with similar
     meaning</br>3. Please check your spelling";
        else {
            echo "$foundnum results found !<p>";

            while ($runrows = mysql_fetch_assoc($run)) {
                $title = $runrows['title'];
                $desc  = $runrows['description'];
                $url   = $runrows['url'];

                echo "
    <a href='$url'><b>$title</b></a><br>
    $desc<br>
    <a href='$url'>$url</a><p>
    ";

            }
        }

    }
}

?>
Dave Chen
  • 10,887
  • 8
  • 39
  • 67
troxie
  • 21
  • 6
  • First check whether your version of PHP supports `mysql_*` functions or not. – Rajdeep Paul Jun 28 '16 at 16:52
  • 6
    mysql_*() functions are deprecated and REMOVED from newer php versions. This code not working is a good thing, because it's riddled with [sql injection attack](http://bobby-tables.com) vulnerabilities. – Marc B Jun 28 '16 at 16:52
  • Have a look at [this](http://stackoverflow.com/a/60496). You'll want to migrate your code to use prepared and executed statements. – Dave Chen Jun 28 '16 at 16:54
  • @Rajdeep Paul The current version of PHP is 7.0.6 – troxie Jun 28 '16 at 16:58
  • As stated in the below answers, `mysql_*` functions are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`pdo`](http://php.net/manual/en/book.pdo.php) instead. [And this is why you shouldn't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). – Rajdeep Paul Jun 28 '16 at 17:01
  • @Rajdeep Paul so would it look something like this? mysqli_connect("","",""); mysql_select_db(""); I'm not very knowledgeable with PHP and is all a new learning curve for me, – troxie Jun 28 '16 at 17:10
  • @troxie If you're new to PHP, I strongly suggest you to learn `mysqli` or `PDO`(preferably `PDO`), and then dive into coding. – Rajdeep Paul Jun 28 '16 at 19:49

2 Answers2

3

You are probably using PHP>=7 where the mysql_ functions are no longer deprecated and have been removed, try using mysqli_ or PDO instead.

Roshan Bhumbra
  • 540
  • 1
  • 8
  • 17
  • There's going to be an influx of these questions soon. Perhaps Stackoverflow could check code segments in past answers so that that "mysql_ functions are deprecated and no longer advised to be used" as theres thousands of valid accepted answers from years back that use `mysql_` – Jamie Bicknell Jun 28 '16 at 16:56
  • I like that idea, perhaps check for new submissions using the `mysql_` functions and suggest they use `mysqli_` also? – Roshan Bhumbra Jun 28 '16 at 17:00
  • I was thinking more of historic Q and A's, where a lot of newcomers would get their starting code from, but also at the time of submission would be worthwhile. – Jamie Bicknell Jun 28 '16 at 17:01
  • @jam I think they would both need to be implemented, although the historic ones would probably be a priority. – Roshan Bhumbra Jun 28 '16 at 17:06
  • I've created a [feature request over at meta.stackoverflow.com](http://meta.stackoverflow.com/questions/327031/warnings-on-past-answers-that-use-mysql-functions) if you fancy adding your comments. – Jamie Bicknell Jun 28 '16 at 17:08
1

Check PHP version, try with mysqli_connect()

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include: mysqli_connect() PDO::__construct()

http://php.net/manual/en/function.mysql-connect.php

Rui Costa
  • 800
  • 4
  • 13