This question already has an answer here:
The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead [duplicate]
(1 answer)
Closed 8 years ago.
My host recently upgrade the PHP version and a certain part of my website now shows the following error:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in url/structure/here on line 49
That is referencing the below code:
function DBConnect() {
$this->connectCount ++;
//echo "$this->connectCount<br>";
if ($this->dbType == 'mysql') {
$dbConnect = mysql_connect($this->dbHost, $this->dbUser, $this->dbPasswd) or die ("MySql Connection Failed: " . mysql_error());
mysql_select_db($this->dbName, $dbConnect);
}
if ($this->dbType == 'postgresql') {
$dbConnect = pg_connect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
//$dbConnect = pg_pconnect("host=$this->dbHost port=$this->dbPort dbname=$this->dbName user=$this->dbUser password=$this->dbPasswd") or die ("PostgreSQL Connection Failed: " . pg_errormessage($dbConnect));
}
return $dbConnect;
}
I'm aware the fact that this is because the current way my site connects to MYSQL is now outdated in the new version of PHP but does anyone know how I would update the above code to make this work?
The easier way is to use mysqli_connect(). The syntax is very similar to what you would had with mysql_connect(),which means the changes in your code will be minor and easy to make.
Pdo would be the safest, but if you are trying to get you site back on quickly, the mysqli_* commands will achieve that.
Google (or check on stackoverflow) mysql vs mysqli. You ll find plenty of examples.
Hope this helps.
Good luck
--
Sorry after re-reading i see you asked what needs to be change on your source code. Afraid i cannot help right now as i am responding from a mobile phone :(
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
I used to write some php a few years ago and used adodb to connect to a mysql database.
After reading a bit on stackoverflow I wondered if adodb is still maintained ?
the latest changelog is from 3rd of September 2012.
I started reading the documentation of mysqli. I can get a database connection but can not seem to figure out a way to run a query and get the results of the query back.
Could someone tell me how to run a simple query like "SELECT * FROM Text WHERE TextId=10;" ?
I tried this
$dbconn = new mysqli($Host, $DbAdminUser, $DbAdminUserPassword, $Database);
if ($dbconn->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
echo $dbconn->host_info . "\n";
$stmt = mysqli_prepare($dbconn, "SELECT * FROM Text WHERE TextId=10;")
$result = mysqli_query($dbconn,$stmt);
print_r($result);
anybody could help me out or point me what I am doing wrong ?
or should I just stick with adodb ?
Please note that mysqli is a low-level api that isn't intended to be used in the application code. So - it's incomparable to ADODB. So, instead of raw mysqli I'd suggest you to use PDO or a library built on top of mysqli, such as my safeMysql
This question already has an answer here:
PHP MySQLi doesn't recognize login info
(1 answer)
Closed 9 years ago.
So I have this SQL query and when I connect the php and MySQL it works but then when I try to run it, the query has an error saying it's using the wrong username/password. But the only place I entered the username and password was when connecting, and then I used the right information and it worked? Here is the code and the error:
<?php
$con = mysqli_connect("Correct Host","Correct Username","Correct Password","Correct Database");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysql_query("SELECT username, email from Profiles");
mysqli_close($con);
?>
Error:
Warning: mysql_query() [function.mysql-query]: Access denied for user 'wronguser'#'wronghost' (using password: NO) in /home/username/public_html/register.php on line 8
You're using the wrong function to query. You connected via mysqli but then you try and query with mysql. Change your code to:
mysqli_query($con, "SELECT username, email from Profiles");
You can't switch from mysqli_* extension to mysql_* like that. Use mysqli_query.
You've used MySQLi to connect to the database but you used MySQL to run the query. Take note, in PHP, MySQL and MySQLi are different. MySQLi is the newer and improved version of MySQL. So use mysqli_query() instead of mysql_query since you've connected to the database using MySQLi.
Hope this helps!
This question already has answers here:
mysqli or PDO - what are the pros and cons? [closed]
(13 answers)
Closed 9 years ago.
So I have this Amazon Web Service database all set up
I'm working off an old tutorial for an application that I'm planning on using it with.
I noticed that mysql_connect was deprecate when I looked it up.
What can I use as an alternative? How can I connect to my amazon database?
<?
mysql_connect("localhost", "username", "password") or die ("Can't connect to database server");
mysql_select_db("videoRecorderDemo") or die ("Can't select database");
mysql_query("SET NAMES 'utf8'");
?>
I get this error:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'username'#'192.168.1.1' (using password: YES) in /www/zxq.net/r/e/d/red5vptest/htdocs/utility/db.php on line 2
Can't connect to database server
No matter what credentials I use.
It also doesn't help that Amazon's code samples show you connecting in an entirely different way.
The documentation for PHP says
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
The error message you are getting is unrelated to the deprecation of mysql_connect. The MySQL username and password you're using ("username" and "password" in your code) are wrong — if you don't know the correct values, check the documentation for the version of MySQL you installed. (You may need to create a user and/or database.)
As several others have mentioned, the supported alternatives to the mysql extension are mysqli and PDO MySQL.
MysqlI (mysql improved) is the successor for mysql.
$Link = new mysqli($Hostname, $Username, $Password, $Database);
$Result = $Link->query('SELECT ...');
$Rows = $Result->fetch_array(MYSQLI_NUM);
One easy alternative to the MySQL extension is MySQLi. Instead of
mysql_connect("host", "username", "password")
... you could connect using e.g.
$db = mysqli_connect("host", "username", "password");
It's probably worth reading this helpful tutorial:
http://phpmaster.com/avoid-the-original-mysql-extension-1/
The manual suggests mysqli as an alternative
http://php.net/manual/en/book.mysqli.php
This question already has answers here:
what is the alternate function mysql_list_tables() in php 5
(5 answers)
Closed 9 years ago.
I'm trying to add a voting system for my website, and I get this error:
Deprecated: Function mysql_list_tables() is deprecated in /home/yokoscap/public_html/vote/classes/vote.class.php on line 11
Access denied for user 'yokoscap'#'localhost' (using password: NO)
I'm new to PHP and MySQL, so if you could dumb it down/help me fix it it'd be great.
Here's the code paste, I thought it was too big to go in here so I uploaded to pastebin.
Did you check the manual? There is a complete example on a replacement for it:
<?php
$dbname = 'mysql_dbname';
if (!mysql_connect('mysql_host', 'mysql_user', 'mysql_password')) {
echo 'Could not connect to mysql';
exit;
}
$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_row($result)) {
echo "Table: {$row[0]}\n";
}
mysql_free_result($result);
use this sql query
show tables from dbname
hope this will help
The mysql_list_tables() function has been deprecated since PHP 4.3.7 (!) because it's entirely too specific.
To get a list of tables in the current database, run a SHOW TABLES query. It's a perfectly normal query; you don't need a special function just to do that.
I says here in the manual
This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
According to mysql_list_tables ...changelog secton
This function became deprecated from version 4.3.7
Use SHOW TABLES