The historical mysql_connect() function opened a MySQL connection in PHP 4/5. That extension no longer exists in PHP 7 or PHP 8. Use PDO or MySQLi for current code.
<?php
$link = mysql_connect($servername, $dbuser, $dbpassword);
?>
Old tutorials commonly followed it with mysql_select_db() and sometimes used mysql_pconnect() for persistent connections. Both belong to the removed extension.
<?php
// Historical only: removed from PHP 7+
$link = mysql_pconnect($servername, $dbuser, $dbpassword);
?>
<?php
$dsn = 'mysql:host=localhost;dbname=sql_tutorial;charset=utf8mb4';
$dbo = new PDO($dsn, 'userid', 'password', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
?>
The database name and charset are part of the DSN, so a separate mysql_select_db()-style call is unnecessary.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$connection = new mysqli('localhost', 'userid', 'password', 'sql_tutorial');
$connection->set_charset('utf8mb4');
?>
See the dedicated PDO connection and MySQLi connection tutorials for application setup.
The old mysql_pconnect() behavior should not simply be copied into a modern application. Persistent connections have different lifecycle and deployment implications. Start with normal PDO/MySQLi connections unless you have a measured reason to use persistence.
Next: closing or releasing a MySQL connection.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.