Uncaught Error: Call to Undefined Function mysql_connect()

Asked

Viewed 2,633 times

0

I made the creation of an html form accompanying a connection file and receiving data from this form (in php), however, when clicking on the input that calls the action receives, I get the following error:

Uncaught Error: Call to Undefined Function mysql_connect()

the codes are like this:

html:

<label>Nome</label><br />
    <input type="text" name="nome" /><br />
<label>Idade</label><br />
    <input type="number" name="idade" /><br />
<label>Telefone</label><br />
    <input type="text" name="telefone" /><br />
<p>
    <input type="submit" value="Cadastrar" />
</p>

php connection.

$con = mysql_connect("localhost", "root", ""); $db = 
mysql_select_db("videoaula", $con);

if(!$con) {     die("Não foi possível conectar ao banco de dados; " . mysql_error()); }

mysql_query("SET NAMES 'utf-8'"); mysql_query('SET character_set_connecton=utf-8'); mysql_query('SET character_set_client=utf-8'); mysql_query('SET character_set_results=utf-8');

php.:

require_once("connection/conexao.php");


$nome = $_POST["nome"];
$idade = $_POST["idade"];
$telefone = $_POST["telefone"];


$sql = "INSERT INTO `clientes` (`nome`, `idade`, `telefone`) VALUES ('$nome', '$idade', '$telefone')";

if(!$sql) {
    echo("Houve um erro na inserção do banco de dados");
        } else 
    {
        echo("Dados inseridos com sucesso");
    }

thanks since you

  • If you are using PHP 7, the functions mysql_* have been removed. Use functions mysqli_* in place.

  • possible duplicate https://answall.com/questions/173037/como-resolvero-erro-call-to-undefined-function-mysql-connect and here https://answall.com/questions/579/por-que-n%C3%A3o-should-use-fun%C3%A7%C3%B5es-do-tipo-mysql

  • The mysql_connect() function has been discontinued.

1 answer

1


The function mysql_connect() was deprecated in PHP 5.5.0 and functions mysql_ * were removed in PHP7!

PHP offers the mysqli and PDO Apis to connect to a Mysql server.

With Mysqli the connection can be made as follows:

$servername = "localhost";
$username = "USUARIO";
$password = "SENHA";
$dbname = "videoaula";

$con = new mysqli($servername, $username, $password, $dbname);

if (!$con) {
    die("Não foi possível conectar ao banco de dados" . mysqli_connect_error());
}

And it has to change too:

$sql = "INSERT INTO `clientes` (`nome`, `idade`, `telefone`) VALUES ('$nome', '$idade', '$telefone')";

$result = mysqli_query($con,$sql);

mysqli_query("SET NAMES 'utf-8'"); 
mysqli_query('SET character_set_connecton=utf-8'); 
mysqli_query('SET character_set_client=utf-8'); 
mysqli_query('SET character_set_results=utf-8');
  • Thank you very much friend, I preferred to redo and everything worked out this way. Thanks!

Browser other questions tagged

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