Asked  7 Months ago    Answers:  5   Viewed   142 times

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:xampphtdocstaskmedianewconnect.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?

 Answers

24

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

Tuesday, June 1, 2021
 
o_flyer
answered 7 Months ago
73

The problem is with PHP variable scoping. Add this line inside of allServers() function before you refer to the $link variable for the first time:

global $link;

See more here: http://php.net/manual/en/language.variables.scope.php

Wednesday, March 31, 2021
 
SheppardDigital
answered 9 Months ago
47

the entire ereg family of functions are deprecated in PHP and will at some point be removed from the language. The replacement is the preg family. For the most part, the change is simple:

preg_replace('/[^<>]>/i', '', $question);
^--           ^      ^^
  1. change ereg to preg
  2. add delimeters (/)
  3. for case insensitive matches (eregi), add the i modifier
Wednesday, March 31, 2021
 
ChronoFish
answered 9 Months ago
26

You probably need to use the global keyword, otherwise $db is considered a var in local scope.

function sanitize ($data){
    global $db;
    $db->mysqli_real_escape_string($data);
}

function user_exists($usermail){
    global $db;
    $usermail = sanitize($usermail);
    $query = $db->query("SELECT COUNT(userId) FROM users WHERE userEmail= '$usermail' ");
    $check = $query->num_rows;
    return ($check == 1) ? true : false;
}
Monday, August 2, 2021
 
ajreal
answered 5 Months ago
77

You must use preg_match instead of ereg because the last one is deprecated.

Replacing it is not a big deal:

ereg( "[][{}()*+?.\^$|]", $_REQUEST['name'] )

will become:

preg_match( "/[][{}()*+?.\^$|]/", $_REQUEST['name'] )

p.s. I had to modify more than one hundred files while I was porting my old project to PHP 5.3 to avoid manually modifying I've used following script to do it for me:

function replaceEregWithPregMatch($path) {
    $content = file_get_contents($path);
    $content = preg_replace('/ereg(("|')(.+)("|'),/',
                            "preg_match('/$2/',",
                            $content);
    file_put_contents($path, $content);
}

I hope it helps.

Monday, August 23, 2021
 
Aniket Thakur
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share