1

I am trying to connect to mySQL database using this code, but I always get an error saying

Fatal error: Uncaught Error: Call to undefined function mysql_connect()

here is my code;

<?php

        $servername = "";
        $user = "";
        $pwd = "";
        $dbname = "";


    function connexion()
        {
            global $servername, $user, $pwd, $dbname;
            $db=mysql_connect($servername,$user,$pwd) or die("Database connection failed: ".mysql_error());
            mysql_select_db($dbname,$db);
        }
?>

The point is to call for the function connecxion() after the user enter the right inputs. Can you tell me what I am doing wrong please?

chris85
  • 23,846
  • 7
  • 34
  • 51
  • Use `mysqli_*` instead of `mysql_*` – rbr94 Oct 24 '16 at 13:06
  • Are you using PHP 7? – chris85 Oct 24 '16 at 13:06
  • Now that you have exposed your database credentials to the rest of the world you need to go change your passwords or ***you will be hacked***. – Jay Blanchard Oct 24 '16 at 13:06
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Oct 24 '16 at 13:06
  • don't worry the password is not the right one. – Zerkani Youssef Oct 24 '16 at 13:09
  • My connection manager is saying different @ZerkaniYoussef – Jay Blanchard Oct 24 '16 at 13:15
  • What s the point of using your connection manager ? – Zerkani Youssef Oct 24 '16 at 13:20

1 Answers1

0

Use PDO (PHP Data Object ) its secure

<?php
 try {
    $yourHandler = new PDO('mysql:dbhost=127.0.0.1;dbname=yourdbname;', 'user', 'pass'); //creates a new instance of the PDO database class and passes it to your handler and creates database connection 
    $yourHandler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //sets the error mode so you can catch any errors that may occur
}  catch (PDOException $e) {
     echo $e->getMessage(); //echoes the errors
     die(); //kills the page 
}
?>
Francis Sunday
  • 77
  • 1
  • 11