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've been told that I was using an deprecated version of PHP, mysql_query, instead of the newer version mysqli_query, and soon it will be remove. Knowing that, I quickly tried to update all of my old, deprecated codes into the newer code. Doing that, I quickly ran into a problem, but I'm not sure what I'm doing wrong. Please take a look:
<?php
$connect = mysqli_connect('server','username','password','database');
$fetch = mysqli_query($connect,"SELECT username FROM userLogin");
while($row=mysqli_fetch_array($fetch,MYSQLI_NUM)){
//do something
}
?>
output:
Warning: mysqli_fetch_array() expects parameter 2 to be long, string given in /home/a2056400/public_html/test4.php on line 8
I have a feeling the error message has something to do with the conditional statement inside the while loop, but I'm not sure what is wrong with it. Please help in any way. Thank you.
You are not connecting to the database properly.
$connect = mysqli_connect('server','username','password','database');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$fetch = mysqli_query($connect,"SELECT username FROM userLogin");
while($row=mysqli_fetch_array($fetch,MYSQLI_NUM)) {
//do something
}
I am querying a mysql database with php but cannot get it to work on my iMac. In particular, php is unable to connect to the mysql DB. It connects to mysql and selects the DB but then fails. See code below:
if (!mysql_connect($db_host, $db_user, $db_pwd)){
die("I cannot connect to database");
}
if (!mysql_select_db($database)){
die("I cannot select database");
}
$sql = "SELECT FROM ${table} ORDER BY $sql_orderBy";
$result = mysql_query($sql);
if (!$result) {
die("I cannot execute query to show fields from Table: {$table}. Query failed.");
}
For reference, I installed apache/mysql/php with macports. The same php code works on my laptop (same installations), and the query works when I invoke it from within mysql on both computers. All variables are declared. Something with the system config is my best guess, but I even went through a uninstall/install.
Any help would be appreciated!
Your issue is ${table} . This should be {$table} or better still, ".$table."
You also need to say what you are SELECTING:
So:
$sql = "SELECT * FROM ".$table." ORDER BY ".$sql_orderBy;
You can discover issues by using Mysql_error() at the end of the query, for example:
mysql_query($sqlString) or die("line: ".__LINE__.":".mysql_error());
this will output a clear error message regarding your SQL statement. This is not for production and public situations but for development.
Also:
MySQL is deprecated and is no longer supported by PHP or the wider community, it is VERY strongly recommended you take up MySQLi or PDO and use these methods as they are much stronger, less flawed and more efficient delivery of results. They will also be supported in future updates and developments whereas MySQL will not.
I have been working for days now and I am at a dead end. After talking with GoDaddy support I am positive that I have the correct hostname, username/password when I run the script but it still cannot get past die().
Ultimately I am attempting to pull a single question from a database. I have combed this website but nothing i found seems to answer my question. Please help.
<?php
$hostname='localhost';
$username='username';
$password='password';
$dbname='qod';
$usertable='Questions';
$userfield='question';
mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to access the Question of the Day! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname);
# Check If Record Exists
$query = 'SELECT $.userfield FROM $.usertable ORDER BY RAND() LIMIT 1';
$result = mysql_query($query);
if($result){
while($row = mysql_fetch_array($result)){
$name = $row[$yourfield];
echo "Name: ".$name;}
}
?>
You are using dots for your SELECT variables where there shouldn't be any.
SELECT $.userfield FROM $.usertable, then calling them with:
$usertable='Questions';
$userfield='question';
Remove them from your SELECT and use proper error reporting techniques such as :
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of code
and
or die('Could not connect: ' . mysql_error());
also a dollar sign in [$yourfield] for your column name; remove it also.
You should be using quotes: I.e.: ['yourfield'] - yourfield being the column name in your table that you wish to show.
Footnotes:
mysql_* functions deprecation notice:
http://www.php.net/manual/en/intro.mysql.php
This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.
These functions allow you to access MySQL database servers. More information about MySQL can be found at » http://www.mysql.com/.
Documentation for MySQL can be found at » http://dev.mysql.com/doc/.
Use mysqli_* with prepared statements, or PDO with prepared statements.
I just started to work on informix.( I have been using mysql all my life)
how i substitute mysql_fetch_assoc for informix.
Lets say I want to display one item at a time.normally in mysql i would run a while loop like this:
while($row=mysql_fetch_assoc($sql))
{
echo $row['name'];
echo $row['number'];
}
How do i do that for informix?
$sql="select * employee";
$result=$dbh->dbRequest("$sql")
I would suggest using PDO with the informix driver. The manual has plenty of examples of executing and returning data from queries using PDO.
PDO Informix driver - http://php.net/manual/en/ref.pdo-informix.php
PDO prepare - http://www.php.net/manual/en/pdo.prepare.php
$db = new PDO('informix:DSN=db', '', '');
$query = $db->prepare('SELECT * FROM employee');
$query->execute();
while ($employee = $query->fetchObject()) {
echo $employee->name;
echo $employee->number;
}
This is a basic Perl DBI question, though there isn't a dbRequest method in the DBI, at least not AFAIK.
You should be using something like this, though I'm assuming you have $dbh->{RaiseError} = 1 or are otherwise going to add error checking to the code:
my $sql = "select * from employee";
my $sth = $dbh->prepare($sql);
$sth->execute;
my $hashref;
while ($hash_ref = $sth->fetchrow_hashref())
{
print "$hash_ref->{name}\n";
print "$hash_ref->{number}\n";
}
This is DBMS-neutral; if you're using Perl DBI with MySQL, Informix, Oracle, DB2, ... this is the way you'd write the code.
NB: I edited the tags to add Perl and DBI and MySQL (and dropped the SQL and syntax tags). If I've misinterpreted your question and you're using PHP instead of Perl, then you should have tagged the code with PHP in the first place (and should now retag it). There is no SQL issue worth mentioning here; it is all about how to use SQL in the (formally unidentified) host language.
There's a different answer if the question is PHP, using the PDO system.