Call to undefined function mysql_pconnect() after a PHP upgrade
Fatal error: Call to undefined function mysql_pconnect()Short answer
The old mysql_* functions belonged to the ext/mysql extension, which PHP removed in version 7.0. On any current PHP they do not exist, so the call fails. In CodeIgniter, change the database driver from mysql to mysqli. In your own code, rewrite the calls with mysqli or PDO.
Fix it fast
1Find out which cause you have
Ask PHP which MySQL extensions are loaded. If mysqli or pdo_mysql is listed but mysql is not, the old functions cannot work.
php -m | grep -i mysql2Match what you see, then jump to its fix
What does the error look like?
Fatal error: Call to undefined function mysql_pconnect()Call to undefined function mysql_connect()
Cause 1 of 2
CodeIgniter is still configured with the mysql driver
1Check
Open the database config and read the dbdriver value.
grep -n "dbdriver\|pconnect" application/config/database.php2Fix
Set dbdriver to mysqli. The CodeIgniter manual gives mysqli as the MySQL value, in lower case.
$db['default']['dbdriver'] = 'mysqli';3Verify
Load any page that touches the database.
curl -I http://localhost/index.phpConfirmed by codeigniter.com
Cause 2 of 2
Your own code calls the removed mysql_* functions
1Check
Search the project for the old calls, including libraries under vendor-style folders.
grep -rn "mysql_[a-z_]*(" --include=*.php .2Fix
Rewrite them with mysqli. A persistent connection is the same pooled kind of connection that mysqli supports; it is controlled by the mysqli.allow_persistent directive.
$db = new mysqli('localhost', 'user', 'password', 'database');
if ($db->connect_errno) {
exit('Connection failed: ' . $db->connect_error);
}
$result = $db->query('SELECT id, name FROM users');
while ($row = $result->fetch_assoc()) {
echo $row['name'];
}3Verify
Run the script from the command line.
php -l script.php && php script.phpConfirmed by php.net
How do I stop it coming back?
- Run php -l and a search for mysql_ calls before changing the PHP version of a server.
- Keep database settings in one config file so one change moves the whole app to mysqli.
- Pin the PHP version in your deployment notes so framework and PHP stay in step.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
CodeIgniter 4: try/catch does not catch a failed insert in the model
DatabaseException
CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask.
3 causes, each with a fixBack end
CodeIgniter 4 where() builds a different condition than you meant
A SQL syntax error that appears only when a value contains a quote character
The query builder joins every where() with AND and every orWhere() with OR, with no brackets.
3 causes, each with a fixBack end
CodeIgniter cannot connect to the MySQL database server
Unable to connect to your database server using the provided settings.
CodeIgniter only reports that its connection failed.
4 causes, each with a fixBack end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fixBack end
Laravel: distinct() and count() give the total instead of the unique count
select('city')->distinct()->count() ignores the select
Laravel builds the count from the column you pass to count(), not from select().
2 causes, each with a fix