MYSQL extension error

Asked

Viewed 687 times

0

I set up a code page on my site that looks for information from the database but in the file that connects to DB and pulls the pertinent information this giving a supposed extension error. This error does not appear on the localhost only when it is hosted on an online server. The error generated is this:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /CSS/function/conecta.coment.php on line 22

The code of the page:

<?php

//base de dados de teste

$dbhost = 'mysql.hostinger.com.br'; // endereco do servidor de banco de dados

$dbuser = 'u28*******_fraex'; // login do banco de dados

$dbname = 'u28*******_fraex'; // nome do banco de dados a ser usado

$dbpass = '**********'; // senha

$conecta = mysql_connect($dbhost, $dbuser, $dbpass, $dbname);

$seleciona = mysql_select_db($dbname);

// cria a instrução SQL que vai selecionar os dados

$query = sprintf("SELECT user_name, comentario FROM comentario ORDER BY coment_id desc limit 5");

// executa a query

$dados = mysql_query($query, $conecta) or die(mysql_error());

// transforma os dados em um array

$linha = mysql_fetch_assoc($dados);

// calcula quantos dados retornaram

$total = mysql_num_rows($dados);
?>

How can I fix this?

  • This type of connection is no longer used and is discouraged as it is too insecure for SQL Inject, use PDO instead for being safer.

  • and how I could change it to PDO?

1 answer

0

This message is not an error but a "warning".

The function mysql will be removed from PHP very soon and the message is suggesting you use mysqli_ or PDO.

You can learn more about the mysqli in the official PHP documentation, and can also consult how to use the PDO which is the most recommended.

  • The more I would need to change some terms or I would have to restructure the codes?

  • Just a few terms, you came to open the link I put in the post? in it has an example of mysqli_connect that you should use instead of mysql_connect and so on, see the section you use with mysql_ and look for the mysqli_ corresponding.

Browser other questions tagged

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