Jump to content
Larry Ullman's Book Forums

Error Message: The Mysql Extension Is Deprecated ... Use Mysqli Or Pdo Instead


Recommended Posts

I created various websites (based on Larry's code) that worked fine for years, but after my web hosting company upgraded to PHP Version 5.6.27, I repeatedly receive this error message:

 

"An error occurred in script '.../mysql_connect.php' on line 8:
mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead"

 

I was able to suppress that error message on one website by prepending  the "@" symbol to the expression, but strangely it did not work on another site. I'd like to know why (as a quick fix), and whether I can do something to suppress error messages for an entire site instead of using "@" multiple times.

 

I'd also love to know how to change my code to work with PDO (preferably) or MySQLi; my web host supports both. It would be great to see an example of PHP handling data with old MySQL code and PHP working with PDO or MySQLi, to see how to "translate" it, so to speak. However, I don't want to use PDO if that requires me to learn Object-Oriented PHP; all my sites are based on procedural PHP.

 

The above error message is generated in response to this mysql_connect.php code:

<?php
error_reporting(0); // didn't suppress error message
ini_set('display_errors',0); // didn't suppress error message
DEFINE ('DB_USER', '***');
DEFINE ('DB_PASSWORD', '***');
DEFINE ('DB_HOST', '***');
DEFINE ('DB_NAME', '***');
if ($dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD)) { // this is the line generating the error message
if (@!mysql_select_db (DB_NAME)) {
trigger_error("Could not select the database!\n<br />MySQL Error: " . mysql_error());
exit();
}
} else {
trigger_error("Could not connect to MySQL!\n<br />MySQL Error: " . mysql_error());
exit();
}
function escape_data ($data) {
if (ini_get('magic_quotes_gpc')) {
$data = stripslashes($data);
}
if (function_exists('mysql_real_escape_string')) {
global $dbc;
$data = @mysql_real_escape_string (trim($data), $dbc);
} else {
$data = @mysql_escape_string (trim($data));
}
return $data;
}
?>

Thank you!

 

Link to comment
Share on other sites

Larry, I ordered the latest version of your book. From Amazon's "Look inside" feature, I'm sure it will answer my MySQLi questions (not sure if it delves into PDO, which would be a great thing for you to cover -- do you do it in the 4th edition or elsewhere?).

 

In pondering this, I think you are missing a golden opportunity. You excel as an author (hence why I've bought several of your books), but you could be a titan of the computer world by offering a service I've never before heard of (and never imagined possible until a few minutes ago), for which there would be tremendous demand. If I have time, I might code a demo of it this winter.

Link to comment
Share on other sites

Hey Doc! Thanks for the order. I really appreciate it. And thanks for the nice words. I do discuss PDO in my PHP Advanced book. The PHP & MySQL uses MySQLi. 

 

To be clear, PDO is OOP and MySQLi can be used via OOP or procedural approaches. But you can still use PDO as OOP within an otherwise procedural application. 

 

Thanks again!

Link to comment
Share on other sites

 Share

×
×
  • Create New...