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>
    ";

            }
        }

    }
}

?>
7
  • First check whether your version of PHP supports mysql_* functions or not. Commented Jun 28, 2016 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 vulnerabilities. Commented Jun 28, 2016 at 16:52
  • Have a look at this. You'll want to migrate your code to use prepared and executed statements. Commented Jun 28, 2016 at 16:54
  • @Rajdeep Paul The current version of PHP is 7.0.6 Commented Jun 28, 2016 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 or pdo instead. And this is why you shouldn't use mysql_* functions. Commented Jun 28, 2016 at 17:01

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

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_
I like that idea, perhaps check for new submissions using the mysql_ functions and suggest they use mysqli_ also?
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.
@jam I think they would both need to be implemented, although the historic ones would probably be a priority.
I've created a feature request over at meta.stackoverflow.com if you fancy adding your comments.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.