Use mysqli or PDO instead? - php

I just uploaded my very old scripts on my hosting account. When I login to my website, I found this note. I read about mysqli but still don't work on changing Anyone please help!
$con = mysql_connect('localhost', 'user_log', '12345');
$db = mysql_select_db('name_log',$con);

The entire ext/mysql PHP extension was officially deprecated in PHP v5.5.0 and removed in PHP v7.There are two other MySQL extensions that you can consider: MySQLi and PDO_MySQL, either of which can be used instead of ext/mysql.
it's possible to suppress deprecation errors by setting error_reporting in php.ini or in your each pages or config file to exclude E_DEPRECATED.
error_reporting = E_ALL ^ E_DEPRECATED
or
you can use
error_reporting(0)
Change your code From
$con = mysql_connect('localhost', 'user_log', '12345');
to
$con= mysqli_connect($db_host,$db_user,$db_pass,$db_database);

Related

Compiled PHP 7 missing mysql extension in WordPress

I have built PHP 7 with a configuration that worked for a previous version of PHP.
Now my WordPress websites get the message:
Your PHP installation appears to be missing the MySQL extension which is required by WordPress.
Other websites using mysqli do work. What am I missing?
I've also included all .so files with mysql in the name:
extension=dba.so
extension=mysql.so
extension=mysqli.so
extension=mysqlnd_mysql.so
extension=mysqlnd_mysqli.so
extension=mysqlnd.so
extension=pdo.so
extension=pdo_mysql.so
extension=pdo_odbc.so
extension=odbc.so
PHP 7 has removed mysql_* completely.
You need to use PDO or mysqli. Wordpress seems not to support this.
mysql_* functions got deleted in PHP 7.0 update your code to mysqli or PDO
Also take a look at prepared statements if you are handling user input. To reduce the chance of SQL injections
An example of mysqli connection string:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
An example of pdo connection string:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>
Note:
That mysqli example handles a connection error
As has been mentioned elsewhere, the ext/mysql functions have been removed. We've been talking about this for some time.
ext/mysql was built for MySQL 3.23 and only got very few additions since then while mostly keeping compatibility with this old version which makes the code a bit harder to maintain.
If you're hell-bent on putting them back in, you can add them back to PHP 7 by using the ext/mysql PECL Library
It's important to note that Wordpress 3.9 or later supports mysqli
In WordPress 3.9, we added an extra layer to WPDB, causing it to switch to using the mysqli PHP library, when using PHP 5.5 or higher.
Check if the Wordpress still using the Mysql extension that was removed in PHP7.
http://php.net/manual/en/migration70.removed-exts-sapis.php
The Mysqli and PDO extensions were kept. This is the reason your other websites are working.
This issue is caused by php 7.1.0-dev.
I built another one with the same configuration version 7.0.0 and the issue was resolved.
This has nothing to do with WordPress since it will automatically try to use MySQLi when MySQL is not found. At least in WP 4.4.
On Ubuntu, I fixed this error by running
sudo apt-get install php-mysql
And then restarting my server (caddy, but you might be using apache or nginx).
source

Wampserver Upgrading issue

I recently updated my wampserver 2.0 to wampserver 2.5.
And while i am running the php smarty code i am getting this error.
"Fatal error: Class 'DB' not found in
C:\wamp\www\livehrm.new\product\common.php on line 63"
I think it might be a pear issue of old wampserver.
Please help me
//session_start();
require_once 'DB.php';
$dbHost = $dbconfig['db_hostname'];
$dsn1[0] = array('type'=>'DB', 'dsnstuff'=>"mysql://$dbUser:$dbPassword#localhost/$dbName"); $dbdsn = "mysql://$dbUser:$dbPassword#$dbHost/$dbName";
$db = DB::connect($dbdsn);
if (DB::isError($db)) {
die ($db->getMessage());
}
?>
MySQL 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. Ref: http://php.net/manual/en/function.mysql-select-db.php
Update your DB.php with following code
<?php
$con = mysqli_connect("localhost","root","");
$db="livehrm";
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con, $db) or die ("Database connection error:\n" . mysqli_error($con));
?>
There is not Class found in you provided code of file DB.php. I will update my answer when you send class code of DB
Its not the prefered solution, but this should get you working again.
I strongly recommend that you do in fact convert your code from using the mysql_* extension ot use either the mysqli_* or PDO instead as eventually this approach will not work as the developers of PHP will actually no longer provide the mysql_* extension.
But you can remove the warning messages by changing your php.ini configuration like this :-
Edit php.ini
Find this line
error_reporting = E_ALL
and change it to
error_reporting = E_ALL & ~E_DEPRECATED
Or you can just add this PHP code at the top of your db.phpscript if you dont have access to your php.ini file
error_reporting( E_ALL ^ E_DEPRECATED );
If you are using Virtual Hosts you can actually add this to the specific VH for the site that is still using the old mysql_* extension therefore not affecting other site you may be developing/maintaining
Unfortunately in a VH definition you have to use an integer value.
<VirtualHost....>
. . .
php_value error_reporting 24575
</VirtualHost>

Hiding Deprecated Error Message Not Working

I was getting this error since I was using a third party application using mysql_ prefix.
Deprecated: The mysql extension is deprecated and will be removed in
the future: use mysqli or PDO instead in /path/to/filename.php on line
123
So i tried to hide this error by editing my php.ini file. I have tried adding
error_reporting = E_ALL ^ E_DEPRECATED;
But that didn't work.
So I tried this
error_reporting(E_ALL ^ E_DEPRECATED);
That too didn't work. What may be the issue? Is it with my php.ini file, or am I doing it wrong. Please let me know the correct method to hide this deprecated error message (or what was wrong with the methods i used).
You need to logically AND the negated E_DEPRECATED flag in order to disable it:
error_reporting(E_ALL &~ E_DEPRECATED);
Try this to deprecate error messages of 3rd third party tools.
error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);

mysql_connect error

In the following code I see echo1 statement, after which I do not see anything printed on the UI.The username and password is correct. But PHP doesn't seem to connect to MySQL. Don't even see the die statement what am I doing wrong. After mysql_connect is encountered the rest of the code doesn't work:
<?php
echo "echo1==========";
$con = mysql_connect("localhost","root", "xxxx123") or die("Error connecting to database");
echo "+++++++++ echo2";
echo $con;
mysql_close($con);
?>
You should be mising an error. Add :
ini_set('display_errors', 1);
error_reporting(E_ALL);
at the beggining of your script
No output means a fatal error. The only possible fatal error is "undefined function mysql_connect (unless something's really messed up somewhere). This means the mysql library is not installed, or it might just not be enabled in the php.ini file.
Check said file, and while you're at it turn error_reporting on.
If you use your code just like this then it's vulnerable for SQL Injection. I would strongly recommend using mysql_real_escape_string as you insert data into your database to prevent SQL injections, as a quick solution or better use PDO or MySQLi.
If you are going to use mysql_* then I'd recommend reading the PHP manual chapter on the mysql_* functions,
where they point out, that this extension is not recommended for writing new code. Instead, they say, you should use either the MySQLi or PDO_MySQL extension.
I also checked mysql_connect and found a weird regularity which is - if you use " on mysql_connect arguments, then it fails to connect and in my case, when I was testing it, it happened just described way, so, please try this instead:
$con = mysql_connect('localhost','username','password');
Replace " to ' as it's shown in the PHP Manual examples and it might work!
EDITED
For those who downvote - TRY first! I tested it on my server with " and it gave me an error: Warning: mysql_connect(): Access denied for user. I use PHP version 5.4.6!
Login to your server with SSH and run php --modules - if you don't see mysql in the list - then it's the reason of your fatal error.
The issue was that the ph5-mysql driver was not installed .Installed it and got it working..Now mysql_connect() function works..Thanks for all the help
For Tomcat 7, the default dir for php.ini is actually C:\windows. So modified (enabling modules, extensions etc.) php.ini should be copied to C:\windows. Then run phpinfo();. It should work fine even on Tomcat 7 along side PHP 5.x. I had this problem and struggled a lot when I wanted the mysql statements to work.
Even after following all instructions from answers in [779246] (Run a php app using tomcat?), mysql statements didn't work (Tomcat 7, PHP 5.2.16, PECL 5.2.5, MySql 5.6 on Windows 7). So I started looking into "phpinfo();" output carefully and used solution as mentioned in earlier paragraph. Please be sure to enable mysql dll extension and also PDO extensions in php.ini if you would like to use PDO (which is future of plain mysql_* methods in PHP).

How to solve the use of deprecated function ereg() of PHP 5.3.0 in Drupal 6.13

Anyone knows how to solve the error below?
Deprecated: Function ereg() is deprecated in C:\wamp\www\includes\file.inc on line 895
It is happening after installing Drupal 6.13 on wamp server 2.0i with PHP 5.3.0
Use
preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);
Instead of
ereg('\.([^\.]*$)', $this->file_src_name, $extension);
Drop your error reporting level below E_DEPRECATED.
PHP 5.3 introduced two new error reporting levels, E_DEPRECATED and E_USER_DEPRECATED and - for the first time in PHP's history - they've started to walk away from older parts of their API. The ereg_* function will still work, but this warning is intended to let you know that "hey, these function will be going away soon, probably in the next major revision).
Just add # in front of the function. e.g.
#ereg()
more issue relating upgraded your web servers which running PHP 5.3.0, pls refer
http://www.rain-forest-forum.com/dotproject-net-installation-issues-t263.html
This is not a Drupal issue.In the Drupal site it is noted that it does not yet support PHP 5.3 and there have been new error flags added to PHP.
Solution1 : You can degarde the PHP version.You can revert back to PHP 5.2.x. As I am unsure of other conflicts with Drupal and PHP 5.3.
Solution2 : However, if you prefer to keep PHP 5.3, you can always suppress the deprecated function errors. In Drupal’s includes/common.inc,
Find the line :
if ($errno & (E_ALL ^ E_NOTICE)) {
And replace it with:
if ($errno & (E_ALL & ~E_NOTICE & ~E_DEPRECATED)) {
This will now always suppress the Deprecated error messages.
One solution is to upgrade the offending sourcecode :-)
It's explained here: http://drupal.org/node/514334#comment-2852940
You can edit you common.inc file to quietly disregard the deprecated error flags. See my post:
http://funkinetics.org/klink/function-ereg-is-deprecated-error-in-drupal-6x-with-php-53/
Looks like the problem is with PHP 5.3.0. You could try downgrading to 5.2.9 as suggested by this drupal link: http://drupal.org/node/514334
Because I don't have time to update legacy code, I addeded following line to php code to suppress warnings.
error_reporting(E_ALL ^ E_DEPRECATED);
this line suppress only deprecated warnings. other errors are shown as usual.

Categories