This is the line that is causing problems.
mysql_connect($dbhost ,$dbuser, $dbpass); mysql_select_db($dbname);
mysql_query("SET NAMES 'utf8'");
mysql_set_charset('utf8');
I just got the code from codecanyon and I don't understand how to fix it. It says that it's deprecated
Please try with example
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Reason:
The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future.
It was originally introduced in PHP v2.0 (November 1997) for MySQL v3.20, and no new features have been added since 2006. Coupled with the lack of new features are difficulties in maintaining such old code amidst complex security vulnerabilities.
The manual has contained warnings against its use in new code since June 2011.
To Fix:
As the error message suggests, there are two other MySQL extensions that you can consider: MySQLi and PDO_MySQL, either of which can be used instead of ext/mysql. Both have been in PHP core since v5.0, so if you're using a version that is throwing these deprecation errors then you can almost certainly just start using them right away—i.e. without any installation effort.
They differ slightly, but offer a number of advantages over the old extension including API support for transactions, stored procedures and prepared statements (thereby providing the best way to defeat SQL injection attacks). PHP developer Ulf Wendel has written a thorough comparison of the features.
Hashphp.org has an excellent tutorial on migrating from ext/mysql to PDO.
I understand that it's possible to suppress deprecation errors by setting error_reporting in php.ini to exclude E_DEPRECATED:
error_reporting = E_ALL ^ E_DEPRECATED
But this is not the professional way of coding.
Quick Answer: Change mysql entries to 'mysqli'.
//Conect to server, now we specify the database in the connect request.
$conn = mysqli_connect($dbhost ,$dbuser, $dbpass, $dbname);
$res = mysqli_query($conn, "SET NAMES 'utf-8'");
$bstat = mysqli_set_charset($conn, 'utf-8');
...
mysqli_free_result($res);
...
mysqli_close($conn);
Not-so-quick Answer: mysql extension is obsolete, it has lots of security holes so PHP 5.5+ dropped support for this extension and further versions won't include it.
You should now use mysqli extension, which come in procedural and object-oriented flavors. Or migrate your code to PDO, which is more versatile.
Related
This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]
(1 answer)
Closed 1 year ago.
I am getting this warning, but the program still runs correctly.
The MySQL code is showing me a message in PHP:
Deprecated: mysql_connect(): The mysql extension is deprecated and
will be removed in the future: use mysqli or PDO instead in
C:\xampp\htdocs\task\media\new\connect.inc.php on line 2
My connect.inc.php page is
<?php
$connect = mysql_connect('localhost','root','');
mysql_select_db('dbname');
?>
What does this mean and how can I eliminate the message?
There are a few solutions to your problem.
The way with MySQLi would be like this:
<?php
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
To run database queries is also simple and nearly identical with the old way:
<?php
// Old way
mysql_query('CREATE TEMPORARY TABLE `table`', $connection);
// New way
mysqli_query($connection, 'CREATE TEMPORARY TABLE `table`');
Turn off all deprecated warnings including them from mysql_*:
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
The Exact file and line location which needs to be replaced is "/System/Startup.php > line: 2 " error_reporting(E_All); replace with error_reporting(E_ALL ^ E_DEPRECATED);
You can remove the warning by adding a '#' before the mysql_connect.
#mysql_connect('localhost','root','');
but as the warning is telling you, use mysqli or PDO since the mysql extension will be removed in the future.
Deprecated features in PHP 5.5.x
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the **MYSQLi or PDO_MySQL extensions.**
Syntax:
<?php
$connect = mysqli_connect('localhost', 'user', 'password', 'dbname');
Also, replace all mysql_* functions into mysqli_* functions
instead of
<?php
$connect = mysql_connect('localhost','root','');
mysql_select_db('dbname');
?>
This warning is displayed because a new extension has appeared.
It suppouse that you still can use the old one but in some cases it´s impossible.
I show you how I do the connection with database. You need just change the values of the variables.
My connection file: connection.php
<?php
$host='IP or Server Name (usually "localhost") ';
$user='Database user';
$password='Database password';
$db='Database name';
//PHP 5.4 o earlier (DEPRECATED)
$con = mysql_connect($host,$user,$password) or exit("Connection Error");
$connection = mysql_select_db($db, $con);
//PHP 5.5 (New method)
$connection = mysqli_connect($host,$user,$password,$db);
?>
The extension changes too when performing a query.
Query File: "example.php"
<?php
//First I call for the connection
require("connection.php");
// ... Here code if you need do something ...
$query = "Here the query you are going to perform";
//QUERY PHP 5.4 o earlier (DEPRECATED)
$result = mysql_query ($query) or exit("The query could not be performed");
//QUERY PHP 5.5 (NEW EXTENSION)
$result = mysqli_query ($query) or exit("The query could not be performed");
?>
This way is using MySQL Improved Extension, but you can use PDO (PHP Data Objects).
First method can be used only with MySQL databases, but PDO can manage different types of databases.
I'm going to put an example but it´s necessary to say that I only use the first one, so please correct me if there is any error.
My PDO connection file: "PDOconnection.php"
<?php
$hostDb='mysql:host= "Here IP or Server Name";dbname="Database name" ';
$user='Database user';
$password='Database password';
$connection = new PDO($hostDb, $user, $password);
?>
Query File (PDO): "example.php"
<?php
$query = "Here the query you are going to perform";
$result=$connection->$query;
?>
To finish just say that of course you can hide the warning but it´s not a good idea because can help you in future save time if an error happens (all of us knows the theory but if you work a lot of hours sometimes... brain is not there ^^ ).
That is because you are using PHP 5.5 or your webserver would have been upgraded to 5.5.0.
The mysql_* functions has been deprecated as of 5.5.0
Source
mysql_*, is officially deprecated as of PHP v5.5.0 and will be removed in the future.
Use mysqli_* function or pdo
Read Oracle Converting to MySQLi
Its just a warning that is telling you to start using newer methods of connecting to your db such as pdo objects
http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338
The manual is here
http://www.php.net/manual/en/book.pdo.php
Warning "deprecated" in general means that you are trying to use function that is outdated. It doeasnt mean thaqt your code wont work, but you should consider refactoring.
In your case functons mysql_ are deprecated. If you want to know more about that here is good explanation already : Why shouldn't I use mysql_* functions in PHP?
PDO class replaces these methods. Example for Mysql or MariaDB :
$BDD_SQL = new PDO('mysql:host='.BDD_SQL_SERVER.';dbname='.BDD_SQL_BASE.';charset=utf8',
BDD_SQL_LOGIN, BDD_SQL_PWD,
array(
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //launch exception if error
PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC
));
Source : PDO Class
<?php
$link = mysqli_connect('localhost','root','');
if (!$link) {
die('Could not connect to MySQL: ' . mysqli_error());
}
echo 'Connection OK'; mysqli_close($link);
?>
This will solve your problem.
If you have done your coding then
ini_set("error_reporting", E_ALL & ~E_DEPRECATED);
is good option but if you are in beginning then definitely you should use mysqli.
Well, i just faced such message today when i moved to new hosting! anyway i have tried to change the "mySQL" to "mySQLi" but not working, so i have done this:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
# Turn off all error reporting
error_reporting(0);
$connect_myconn = "Database Connection";
$hostname_myconn = "localhost";
$database_myconn = "db name";
$username_myconn = "user name";
$password_myconn = "pass";
$myconn = mysql_connect($hostname_myconn, $username_myconn, $password_myconn) or die("<h1 style=margin:0;>A MySQL error has occurred.</h1><p><b>Your Query:</b> " . $connect_myconn . "<br /> <b>Error Number:</b> (" . mysql_errno() . ")</p>" . mysql_error());
mysql_select_db($database_myconn, $myconn);
?>
The trick is to set error reporting off :)
# Turn off all error reporting
error_reporting(0);
For PHP 7+ you can use this code instead:
ini_set('display_errors', 0);
ini_set('log_errors', 1);
Thanks
In the light of MySQL to soon be deprecated, I need to move a large website using ADODB from MySQL to MySQLi.
Now I have looked up a few topics on Stackoverflow and thanks to the community I already have a genral idea of what needs to be done. Best topics on the matter are those ones:
ADODB mySQLi Connection
Switch large website from MySQL to MySQLi
However, I do still need a bit more clarification on my particular case, where ADODB is being used.
This is what I use to connect to the DB:
define('DBHOST', 'db_host');
define('DBUSER', 'db_user');
define('DBPASS', 'db_pass');
define('DBNAME', 'db_name');
include('adodb/adodb.inc.php');
$db = ADONewConnection('mysql');
$db->Connect(DBHOST,DBUSER,DBPASS,DBNAME) or die("Database not found!");
So first I am changing:
$db = ADONewConnection('mysql');
to
$db = ADONewConnection('mysqli');
That's the easy part, I guess.
Now since I am using ADODB, do I also need to change all instances of MySQL_* functions to MySQLi_* or ADODB takes care of this automatically? I think I know the answer but anyhow have to ask.
My most common MySQL_ functions are:
mysql_insert_id()
mysql_query()
mysql_fetch_array()
mysql_num_rows()
mysql_escape_string()
mysql_connect()
mysql_select_db()
mysql_error()
Most common usage is like $variable = mysql_insert_id(); or $v1 = mysql_query($v);
Is there anything else I should take into consideration when moving from MySQL to MySQLi for ADODB?
"do I also need to change all instances of MySQL_* functions to MySQLi_* ?"
The answer is yes. Different MySQL APIs/functions do not intermix. You must use the same API/functions from connection to querying.
You can use the following functions, simply replacing mysql_ by mysqli_, while passing a database connection in functions that require it and as the first parameter.
I.e. mysqli_query($connection, $query).
They are marked with asterisks *.
mysqli_insert_id() - *
mysqli_query() - *
mysqli_fetch_array()
mysqli_num_rows()
mysqli_escape_string() - *
mysqli_connect() - *
mysqli_select_db() - *
mysqli_error() - *
Consult the manual http://php.net/manual/en/book.mysqli.php
1) I would suggest you to use pdo_mysql instead of mysql since it has better support for transactions.
2) ADODB initializes the driver being used (e.g. mysql or pdo_mysql) by using the dsn identifier from the connection string i.e. pdo_mysql://localhost/mydb or mysql://localhost/mydb.
Just switch mysql -> pdo_mysql in the connection string and it starts to use mysqli -driver instead.
3) Driver loading logic is located on row ~4860 at adodb.inc.php, and at least in the version I'm using from composer, there is no switch to be able to configure mysqli, but it upgrades mysql -> mysqli automatically if you have PHP 7.0.0 or higher. You could tweak this code to force mysqli also by adding one if-statement, but then you have a custom fork of ADODB. Maybe do a pull request.
case 'mysql':
// mysql driver deprecated since 5.5, removed in 7.0
// automatically switch to mysqli
if(version_compare(PHP_VERSION, '7.0.0', '>=')) {
$db = 'mysqli';
}
$class = $db;
break;
This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]
(1 answer)
Closed 1 year ago.
I am getting this warning, but the program still runs correctly.
The MySQL code is showing me a message in PHP:
Deprecated: mysql_connect(): The mysql extension is deprecated and
will be removed in the future: use mysqli or PDO instead in
C:\xampp\htdocs\task\media\new\connect.inc.php on line 2
My connect.inc.php page is
<?php
$connect = mysql_connect('localhost','root','');
mysql_select_db('dbname');
?>
What does this mean and how can I eliminate the message?
There are a few solutions to your problem.
The way with MySQLi would be like this:
<?php
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
To run database queries is also simple and nearly identical with the old way:
<?php
// Old way
mysql_query('CREATE TEMPORARY TABLE `table`', $connection);
// New way
mysqli_query($connection, 'CREATE TEMPORARY TABLE `table`');
Turn off all deprecated warnings including them from mysql_*:
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
The Exact file and line location which needs to be replaced is "/System/Startup.php > line: 2 " error_reporting(E_All); replace with error_reporting(E_ALL ^ E_DEPRECATED);
You can remove the warning by adding a '#' before the mysql_connect.
#mysql_connect('localhost','root','');
but as the warning is telling you, use mysqli or PDO since the mysql extension will be removed in the future.
Deprecated features in PHP 5.5.x
The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the **MYSQLi or PDO_MySQL extensions.**
Syntax:
<?php
$connect = mysqli_connect('localhost', 'user', 'password', 'dbname');
Also, replace all mysql_* functions into mysqli_* functions
instead of
<?php
$connect = mysql_connect('localhost','root','');
mysql_select_db('dbname');
?>
This warning is displayed because a new extension has appeared.
It suppouse that you still can use the old one but in some cases it´s impossible.
I show you how I do the connection with database. You need just change the values of the variables.
My connection file: connection.php
<?php
$host='IP or Server Name (usually "localhost") ';
$user='Database user';
$password='Database password';
$db='Database name';
//PHP 5.4 o earlier (DEPRECATED)
$con = mysql_connect($host,$user,$password) or exit("Connection Error");
$connection = mysql_select_db($db, $con);
//PHP 5.5 (New method)
$connection = mysqli_connect($host,$user,$password,$db);
?>
The extension changes too when performing a query.
Query File: "example.php"
<?php
//First I call for the connection
require("connection.php");
// ... Here code if you need do something ...
$query = "Here the query you are going to perform";
//QUERY PHP 5.4 o earlier (DEPRECATED)
$result = mysql_query ($query) or exit("The query could not be performed");
//QUERY PHP 5.5 (NEW EXTENSION)
$result = mysqli_query ($query) or exit("The query could not be performed");
?>
This way is using MySQL Improved Extension, but you can use PDO (PHP Data Objects).
First method can be used only with MySQL databases, but PDO can manage different types of databases.
I'm going to put an example but it´s necessary to say that I only use the first one, so please correct me if there is any error.
My PDO connection file: "PDOconnection.php"
<?php
$hostDb='mysql:host= "Here IP or Server Name";dbname="Database name" ';
$user='Database user';
$password='Database password';
$connection = new PDO($hostDb, $user, $password);
?>
Query File (PDO): "example.php"
<?php
$query = "Here the query you are going to perform";
$result=$connection->$query;
?>
To finish just say that of course you can hide the warning but it´s not a good idea because can help you in future save time if an error happens (all of us knows the theory but if you work a lot of hours sometimes... brain is not there ^^ ).
That is because you are using PHP 5.5 or your webserver would have been upgraded to 5.5.0.
The mysql_* functions has been deprecated as of 5.5.0
Source
mysql_*, is officially deprecated as of PHP v5.5.0 and will be removed in the future.
Use mysqli_* function or pdo
Read Oracle Converting to MySQLi
Its just a warning that is telling you to start using newer methods of connecting to your db such as pdo objects
http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338
The manual is here
http://www.php.net/manual/en/book.pdo.php
Warning "deprecated" in general means that you are trying to use function that is outdated. It doeasnt mean thaqt your code wont work, but you should consider refactoring.
In your case functons mysql_ are deprecated. If you want to know more about that here is good explanation already : Why shouldn't I use mysql_* functions in PHP?
PDO class replaces these methods. Example for Mysql or MariaDB :
$BDD_SQL = new PDO('mysql:host='.BDD_SQL_SERVER.';dbname='.BDD_SQL_BASE.';charset=utf8',
BDD_SQL_LOGIN, BDD_SQL_PWD,
array(
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //launch exception if error
PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC
));
Source : PDO Class
<?php
$link = mysqli_connect('localhost','root','');
if (!$link) {
die('Could not connect to MySQL: ' . mysqli_error());
}
echo 'Connection OK'; mysqli_close($link);
?>
This will solve your problem.
If you have done your coding then
ini_set("error_reporting", E_ALL & ~E_DEPRECATED);
is good option but if you are in beginning then definitely you should use mysqli.
Well, i just faced such message today when i moved to new hosting! anyway i have tried to change the "mySQL" to "mySQLi" but not working, so i have done this:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
# Turn off all error reporting
error_reporting(0);
$connect_myconn = "Database Connection";
$hostname_myconn = "localhost";
$database_myconn = "db name";
$username_myconn = "user name";
$password_myconn = "pass";
$myconn = mysql_connect($hostname_myconn, $username_myconn, $password_myconn) or die("<h1 style=margin:0;>A MySQL error has occurred.</h1><p><b>Your Query:</b> " . $connect_myconn . "<br /> <b>Error Number:</b> (" . mysql_errno() . ")</p>" . mysql_error());
mysql_select_db($database_myconn, $myconn);
?>
The trick is to set error reporting off :)
# Turn off all error reporting
error_reporting(0);
For PHP 7+ you can use this code instead:
ini_set('display_errors', 0);
ini_set('log_errors', 1);
Thanks
/* Please input your sql-connection correctly
Made by Nitti
*/
$sqlHost = 'localhost';
$sqlUser = 'root';
$sqlPass = '';
// Please don't change anything below
mysql_connect($sqlHost, $sqlUser, $sqlPass) or die('Could not connect to the mysqlserver ('.mysql_error().')');
mysql_select_db('db_nit')
;
and as a result
Fatal error: Call to undefined function mysql_connect() in C:\UwAmp\www\admin\php\connectDb.php on line 13
I'm using `
Apache 2.2.24 (with SSL)
MySQL 5.6.11
PHP (5.3.25 / 5.4.15) with Xdebug 2.2.0
PHPMyAdmin 4.0.2
`
mysql_* functions were deprecated and will then removed from PHP. You need to use the mysqli_* functions or PDO for database operations now.
See PDO manual: http://php.net/manual/en/book.pdo.php
See mysqli manual: http://php.net/manual/en/book.mysqli.php
Default UwAmp MySQL password is "root".
$sqlHost = 'localhost';
$sqlUser = 'root';
$sqlPass = 'root';
Using mysql_connect has been depreciated for a long time. At a guess, they finally got rid of the feature entirely in your version, given that you're receiving an undefined function error.
As another poster also added, you should use either mysqli_* functions, or better yet the PDO library. I strongly recommend the PDO library, but the mysqli library does work as well.
The PDO library will give you the advantage of making your database object oriented and properly abstract away the actual database, giving you the freedom to eventually move onto other databases if the program requires. It might look like a little bit of work to learn, but it's much easier than you think once you dig into it and really start using it.
MySQL connection in PHP can be established in two ways:
$mysql = new mysqli('localhost', 'user_name', 'password', 'database_name');
or
$link = mysqli_connect('localhost', 'user_name', 'password');
mysqli_set_charset($link, 'utf8');
mysqli_select_db($link, 'database_name');
Which one is the better and why?
Whichever one you prefer. I would go with the OOP Interface for consistency with the rest of my application, because that's how I use MySqli. Also, in my opinion, the OOP interface way is much cleaner (aesthetically, at least).
The best way to connect to MySQL database in PHP is using PDO driver. PDO offers you parameterized query that lets you avoid SQL injection easily, and other features you may love. It is ready to work with object-oriented programming, which is pretty cool.
PDO also can be used to connect to other kinds of SQL databases, like SQL Server, etc. You have to learn PDO (it's simple) and then you can connect to many kinds of SQL databases.
Connecting using the mysqli extension gives you the ability to use newer MySQL features such as transactional queries and parameterised queries which aren't available using the older mysql extension.
Have a look at http://www.php.net/manual/en/book.mysqli.php
Will you be dealing with more than one database? If so it might be a good idea not to set the database_name in the constructor. Otherwise, no problem. Other than the fact that you set the charset in the second one I don't think there's much of a difference.
if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
// error: mysqli extension error
exit('...');
}
$connection = mysqli_init();
#mysqli_real_connect($connection, DBHOST,
DBUSER, DBPASS, DBNAME, DBPORT, NULL, MYSQLI_CLIENT_FOUND_ROWS);
if (mysqli_connect_errno() > 0) {
// error: connection error
echo mysqli_connect_error();
exit();
}
// Force UTF-8.
mysqli_query($connection, 'SET NAMES "utf8"');
This sample according to Drupal6
database.mysqli.inc
Both approaches are valid ways to use the mysqli extension. The first is the object-oriented interface and the second is the procedural interface. They are equivalent and it is simply a matter of preference. See the PHP mysqli documentation.