Deprecated: mysql_connect(): The mysql extension is deprecated and will be remov

Deprecated: mysql_connect(): The mysql extension is deprecated and wil
Posted 4 minutes ago
I have created a php rating system for my website. It has been working fine, i have started up the mamp program and i am now getting this error message:
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /Applications/MAMP/htdocs/project1/rate.php on line 3

I have tried several things for it to work but i have had no luck, i have changed the php.ini file error option; display_errors = On to display_errors = Off. I have had no luck please help i have been stuck all evening on this. Thank you in advance.

The two pages which involves this error are below:

The php code for the fglrank.php page is:

<?php mysql_connect("localhost","root","root") or die ("Couldnt connect to server"); mysql_select_db("rating") or die ("Couldnt connect to database"); $find_data = mysql_query("SELECT * FROM rates"); while($row = mysql_fetch_assoc($find_data)) { $id = $row['id']; $name_of_player = $row['nameofplayer']; $pos = $row['pos']; $current_rating = $row['rating']; $hits = $row['hits']; echo " $name_of_player: 1 2 3 4 5 6 7 8 9 10 Current Rating: "; echo round($current_rating, 2); echo " "; } ?>

The rate.php file code is:

<?php mysql_connect("localhost","root","root") or die ("Couldnt connect to server"); mysql_select_db("rating") or die ("Couldnt connect to database"); $pos = $_POST['pos']; $post_rating = $_POST['rating']; $find_data = mysql_query("SELECT * FROM rates WHERE pos='$pos'"); while($row = mysql_fetch_assoc($find_data)) { $id = $row['id']; $current_rating = $row['rating']; $current_hits = $row['hits']; } $new_hits = $current_hits + 1; $update_hits = mysql_query("UPDATE rates SET hits = '$new_hits' WHERE id='$id'"); $pre_rating = $current_rating + $post_rating; $new_rating = $pre_rating / $new_hits; $update_rating = mysql_query("UPDATE rates SET rating = '$new_rating' WHERE id='$id'"); ?>

The error message told you all you need to know. You are using obsolete Mysql calls. Use PDO or mysqli. Problem solved. Error messages are your friend. Dont turn it off.

While you can and should not have errors and warnings on your page, in development you need them. So you do not, should not, want to hide them in your .ini file.

What it is telling you is, your code will break and then you are screwed. Update the code as Kevin said and you will be fine.

Marvelaa, it is easy to change your code from MySQL into the newer MySQLi. (The i stands for Improved.)

Here is a link to how to handle the connection to the database. Follow the small link at the top of that
article to look at the other MySQLi commands to see the differences. They are minor and will not take
long to change them…

Good luck with your project… http://www.w3schools.com/php/func_mysqli_connect.asp

Sponsor our Newsletter | Privacy Policy | Terms of Service