PHP Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /home/username/public_html/db_connection.php:11

The error you're encountering happens because the `mysql_connect()` function is no longer supported in PHP versions 7.0 and later. This function was removed in favor of the newer `mysqli` or `PDO` extensions, which offer better security and performance. However if you still want to use mysql_connect and not the mysqli you might have to consider using older version of php like php5 and required extensions.

To resolve this issue, you can replace `mysql_connect()` with `mysqli_connect()` or refactor your code to use the `PDO` extension. Here's how you can update your code:

Using `mysqli_connect()`

<?php
// Database connection details
$servername = "your_server_name";
$username = "your_username";
$password = "your_password";
$database = "your_database_name";

// Create a connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check the connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>


Using `PDO`

<?php
try {
$dsn = "mysql:host=your_server_name;dbname=your_database_name";
$username = "your_username";
$password = "your_password";

// Create a PDO instance
$conn = new PDO($dsn, $username, $password);

// Set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>


Both approaches should work, depending on your preference. The `mysqli` extension is more similar to the old `mysql` extension, whereas `PDO` offers more flexibility, including support for multiple database types.

  • mysqli, pdo extension, mysql_connect, mysqli_connect, cpanel mysql, pdo
  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

How to reset SSH port back to default port 22?

In some situations most of us may come across a problem that we are able to log into the WHM but...

How to setup the root login alert notification email?

Its very important to keep a check on your servers and who logs into the server, From security...

How to enable Open SSL on WHM?

Before enabling open SSL, you can check whether it is already enabled on your server. Please fire...

How to install mod_pagespeed on WHM?

'mod_pagespeed' is module of Apache which is nothing but plugin which is used for compressing the...

How to enable Hot Link Protection in cPanel?

To avoid other sites to use your website's bandwidth, you can enable the hot link protection....

Powered by WHMCompleteSolution