Fatal error: Uncaught Error: Call to undefined function mysql_connect()

PhpMysqlXampp

Php Problem Overview


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

Php Solutions


Solution 1 - Php

mysql_* functions have been removed in PHP 7.

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

Solution 2 - Php

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.

Solution 3 - Php

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?

Solution 4 - Php

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...

Solution 5 - Php

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

Solution 6 - Php

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

Solution 7 - Php

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

> msyql_fetch_assoc should be mysql

Solution 8 - Php

For mysqli you can use :

$db = ADONewConnection('mysqli');

... ...

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

Solution 9 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDemeteorView Question on Stackoverflow
Solution 1 - PhpefikView Answer on Stackoverflow
Solution 2 - Phpuser6128099View Answer on Stackoverflow
Solution 3 - PhpAbhijit JagtapView Answer on Stackoverflow
Solution 4 - PhpkonzoView Answer on Stackoverflow
Solution 5 - Phpuser664833View Answer on Stackoverflow
Solution 6 - PhpDhaval AjaniView Answer on Stackoverflow
Solution 7 - PhpHammad KhanView Answer on Stackoverflow
Solution 8 - Phpuser1077915View Answer on Stackoverflow
Solution 9 - PhpKumar Pankaj DubeyView Answer on Stackoverflow