Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecated: mysql_connect() [duplicate]

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?

like image 305
Mubashar Ahmed Hassan Avatar asked Feb 15 '14 11:02

Mubashar Ahmed Hassan


People also ask

Is mysql_connect deprecated?

This extension was deprecated in PHP 5.5. 0, and it was removed in PHP 7.0.

What does the mysql_connect () function do?

mysql_connect() establishes a connection to a MySQL server. The following defaults are assumed for missing optional parameters: server = 'localhost:3306', username = name of the user that owns the server process and password = empty password. The server parameter can also include a port number.

What is difference between mysql_connect and Mysqli_connect?

The mysqli_connect() function in PHP is used to connect you to the database. In the previous version of the connection mysql_connect() was used for connection and then there comes mysqli_connect() where i means improved version of connection and is more secure than mysql_connect().

Does PHP 5.6 support mysql_connect?

It's still available in 5.6.


2 Answers

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);

like image 128
Tharindu Kumara Avatar answered Sep 27 '22 20:09

Tharindu Kumara


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.

like image 36
Rebirth Avatar answered Sep 27 '22 18:09

Rebirth