-2

I tried to make a search engine but i got a mysql/PHP error:

Fatal error: Uncaught Error: Class 'mysql_connect' not found in /storage/ssd1/238/3211238/public_html/search.php:12 Stack trace: #0 {main} thrown in /storage/ssd1/238/3211238/public_html/search.php on line 12

My Code:

<?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("localhost", "id3211238_base", "Rijk1234");
        mysqli_select_db("id3211238_data", "id3211238_base");

        $search_exploded = explode(" ", $search);
        $x = 0;
        foreach ($search_exploded as $search_each) {
            $x++;
            $construct = "";
            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>";
            }
        }
    }
}
?>
Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47
PuKool
  • 25
  • 2
  • 5
    All mysql_* functions were (finally) removed in PHP 7. You need to update to use either PDO or Mysqli – JimL Oct 11 '17 at 14:20
  • Learn about prepared Statements to prevent SQL injection – Jens Oct 11 '17 at 14:21
  • Reaction to JimL: but i get another error then: Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /storage/ssd1/238/3211238/public_html/search.php on line 13 – PuKool Oct 11 '17 at 14:22
  • 1
    Read the manual about mysqli... You can't just do a search/replace. People have pointed you in the right direction so read, study and do some proper attempts. – M. Eriksson Oct 11 '17 at 14:23
  • Possible duplicate of [PHP : Undefined function mysql\_connect()](https://stackoverflow.com/questions/34557221/php-undefined-function-mysql-connect) – Matt S Oct 11 '17 at 14:24
  • You are mixing `mysql_*` and `mysqli_*` code, which is not valid. [Can I mix mysql APIs?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – GrumpyCrouton Oct 11 '17 at 14:25
  • **Please**, don't use `mysql_*` functions for new code. They are no longer maintained and the community has begun the [deprecation process](http://news.php.net/php.internals/53799), and `mysql_*` functions have been officially removed in PHP 7. Instead you should learn about [prepared statements](https://en.wikipedia.org/wiki/Prepared_statement) and use either `PDO` or `mysqli_*`. If you can't decide, [this article will help to choose your best option](http://php.net/manual/en/mysqlinfo.api.choosing.php). – GrumpyCrouton Oct 11 '17 at 14:26
  • 2
    Possible duplicate of ["Call to undefined function mysql\_connect()" after upgrade to php-7](https://stackoverflow.com/questions/34088373/call-to-undefined-function-mysql-connect-after-upgrade-to-php-7) – aynber Oct 11 '17 at 14:26
  • [Little Bobby](http://bobby-tables.com/) says **[you may be at risk for SQL Injection Attacks](https://stackoverflow.com/q/60174/)**. Learn about [Prepared Statements](https://en.wikipedia.org/wiki/Prepared_statement) with [parameterized queries](https://stackoverflow.com/a/4712113/5827005). I recommend `PDO`, which I [wrote a function for](http://paragoncds.com/grumpy/pdoquery/#function) to make it extremely easy, clean, and more secure than using non-parameterized queries. Also, [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) may help you choose between `MySQLi` and `PDO` – GrumpyCrouton Oct 11 '17 at 14:26

1 Answers1

0

try the following:

I'm sure there are still going to be bugs but it should get you a lot closer than before.

read the documentation at http://php.net/manual/en/book.mysqli.php

<?php
$db_host = 'localhost';
$db_user = 'root';
$db_password = 'password';
$db_name = 'name';

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

if( !$button )
    die("you didn't submit a keyword");
}
if( strlen( $search ) <= 1 ){
    die("Search term too short");
}
echo "You searched for <b> $search </b> <hr size='1' > </ br > ";
// connect
$db_conn = new mysqli($db_host, $db_user, $db_password, $db_name);
// if errors connecting
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);

$button = real_escape_string($button) ;
$search = real_escape_string($search) ; 

$search_exploded = explode ( " ", $search );
$x = 0; 
foreach( $search_exploded as $search_each ) {
    $x++;
    $construct = "";
    if( $x == 1 ){
        $construct .="keywords LIKE '%$search_each%'";
    }else{
        $construct .="AND keywords LIKE '%$search_each%'";
    }
}
$result = $db_conn->query($construct)

if ($result->num_rows == 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 $result->num_rows." results found !<p>";
    while( $row = result->fetch_assoc()) {
        $title = $row ['title'];
        $desc = $row ['description'];
        $url = $row ['url'];

        echo "<a href='$url'> <b> $title </b> </a> <br> $desc <br> <a href='$url'> $url </a> <p>";
    }
}
$result->close();
$mysqli->close();

When checking for errors, there is no need to put the rest of the code in an else statement, for it creates ugly code. instead use "die('message');"

By the way that was a PHP error.

Community
  • 1
  • 1
Arye Eidelman
  • 1,579
  • 16
  • 22