Simple question from PHP

Asked

Viewed 62 times

-4

I have this line in a code to connect to the database, but the following error appears:

Code

$con=mysql_connect("localhost","root","");

Error

Fatal error: Uncaught Error: Call to Undefined Function mysql_connect() in C: xampp htdocs login bancoDespesas.php:8 Stack trace: #0 {main} thrown in C: xampp htdocs login bancoDespesas.php on line 8

How can I solve?

1 answer

-3


Apparently missing parameters for connection. Below is the method I do.

<?php 

    $DATABASE_HOST = 'localhost';
    $DATABASE_USER = 'root';
    $DATABASE_PASS = '';
    $DATABASE_NAME = 'nome_do_banco';

    $con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
    if ( mysqli_connect_errno() ) {
        header('HTTP/1.1 500 Internal Server Error');
        header('Content-Type: application/json');
        die(json_encode(array('message' => 'Falha ao Conectar ao MySQL: ' . mysqli_connect_error(), 'code' => 599)));
        exit('Falha ao Conectar ao MySQL: ' . mysqli_connect_error());
    }
?>

Here is also an example of the site itself php.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

But I believe that this method only works in php5 if you are using php7 the first and best choice. I hope I have helped.

  • 4

    "Undefined Function mysql_connect()" has nothing to do with connection error, the function is that there is no.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.