83

I am trying to do a simple connection with XAMPP and MySQL server, but whenever I try to enter data or connect to the database, I get this error.

Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\register.php:22
Stack trace: #0 {main} thrown in C:\xampp\htdocs\register.php on line 22

Example of line 22:

$link = mysql_connect($mysql_hostname , $mysql_username);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Demeteor
  • 1,193
  • 2
  • 17
  • 33
  • 11
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – Rizier123 Jan 03 '16 at 16:57
  • Change mysql_connect($mysql_hostname , $mysql_username) to mysql_connect($mysql_hostname , $mysql_username, $mysql_password) . Also, switch to PDO or MySQLi! – Chris G Jan 03 '16 at 17:12
  • 1
    in case that I dont use a password on phpmyadim ? (trying to do all those remote things just cause I need the cellphone to be able to connect to the database) – Demeteor Jan 03 '16 at 17:15
  • 1
    If you come across this error with legacy _CodeIgniter_, use this in your `config/database.php`: `'dbdriver' => 'mysqli',` (change `mysql` to `mysqli`. – ProfNandaa Sep 11 '16 at 12:52

9 Answers9

127

mysql_* functions have been removed in PHP 7.

You probably have PHP 7 in XAMPP. You now have two alternatives: MySQLi and PDO.

donohoe
  • 13,867
  • 4
  • 37
  • 59
efik
  • 1,286
  • 1
  • 10
  • 3
43

You can use mysqli_connect($mysql_hostname , $mysql_username) instead of mysql_connect($mysql_hostname , $mysql_username).

mysql_* functions were removed as of PHP 7. You now have two alternatives: MySQLi and PDO.

user664833
  • 18,397
  • 19
  • 91
  • 140
user6128099
  • 431
  • 4
  • 2
  • 1
    Other changes to your code are likely to be needed, though. – ceejayoz Jan 17 '17 at 17:43
  • 1
    On Ubuntu 16.04, this required the `php7.0-mysql` package. As @ceejayoz says, there are actually several API changes required, and not just to function names but to the order of their arguments. Very frustrating. – Drew Noakes Feb 10 '17 at 20:20
  • Rough one but it worked for me, at least till i optimise my code with PDO connection – adeguk Loggcity Sep 04 '17 at 05:37
25

It is recommended to use either the MySQLi or PDO extensions. It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7.

PHP offers three different APIs to connect to MySQL. Below we show the APIs provided by the mysql, mysqli, and PDO extensions. Each code snippet creates a connection to a MySQL server running on "example.com" using the username "username" and the password "password". And a query is run to greet the user.

Example #1 Comparing the three MySQL APIs

<?php
// mysqli
$mysqli = new mysqli("example.com", "username", "password", "database");
$result = $mysqli->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $result->fetch_assoc();
echo htmlentities($row['_message']);

// PDO
$pdo = new PDO('mysql:host=example.com;dbname=database', 'username', 'password');
$statement = $pdo->query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = $statement->fetch(PDO::FETCH_ASSOC);
echo htmlentities($row['_message']);

// mysql
$c = mysql_connect("example.com", "username", "password");
mysql_select_db("database");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);
?>

I suggest you try out both MySQLi and PDO and find out what API design you prefer.

Read Choosing an API and Why shouldn't I use mysql_* functions in PHP?

user664833
  • 18,397
  • 19
  • 91
  • 140
Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43
14

As other answers suggest... Some guy (for whatever reason) decided that your old code should not work when you upgrade your PHP, because he knows better than you and don't care about what your code does or how simple it is for you to upgrade.

Well, if you can't upgrade your project overnight you can

downgrade your version of PHP to whatever version that worked

or...

use a shim (kind of polyfill) such as https://github.com/dshafik/php7-mysql-shim or https://github.com/dotpointer/mysql-shim, and then find a place for include_once("choice_shim.php"); somewhere in your code

That will keep your old PHP code up and running until you are in a mood to update...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
konzo
  • 1,973
  • 22
  • 32
12

mysql_* functions have been removed in PHP 7.

You now have two alternatives: MySQLi and PDO.

The following is a before (-) and after (+) comparison of a migration to the MySQLi alternative, taken straight out of working code:

-if (!$dbLink = mysql_connect($dbHost, $dbUser, $dbPass))
+if (!$dbLink = mysqli_connect($dbHost, $dbUser, $dbPass))

-if (!mysql_select_db($dbName, $dbLink))
+if (!mysqli_select_db($dbLink, $dbName))

-if (!$result = mysql_query($query, $dbLink)) {
+if (!$result = mysqli_query($dbLink, $query)) {

-if (mysql_num_rows($result) > 0) {
+if (mysqli_num_rows($result) > 0) {

-while ($row = mysql_fetch_array( $result, MYSQL_ASSOC )) {
+while ($row = mysqli_fetch_array( $result, MYSQLI_ASSOC )) {

-mysql_close($dbLink);
+mysqli_close($dbLink);
user664833
  • 18,397
  • 19
  • 91
  • 140
4

mysql_ functions have been removed from PHP 7. You can now use MySQLi or PDO.

MySQLi example:

mysqli_connect($mysql_hostname, $mysql_username, $mysql_password, $mysql_dbname);

mysqli_connect reference link

user664833
  • 18,397
  • 19
  • 91
  • 140
Dhaval Ajani
  • 177
  • 1
  • 6
1

Make sure you have not committed a typo as in my case

msyql_fetch_assoc should be mysql

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
-2

For mysqli you can use :

$db = ADONewConnection('mysqli');

... ...

$db->execute("set names 'utf8'");

user1077915
  • 828
  • 1
  • 12
  • 26
-2

in case of a similar issue when I'm creating dockerfile I faced the same scenario:- I used below changed in mysql_connect function as:-

if($CONN = @mysqli_connect($DBHOST, $DBUSER, $DBPASS)){ //mysql_query("SET CHARACTER SET 'gbk'", $CONN);

Kumar Pankaj Dubey
  • 1,541
  • 3
  • 17
  • 17