Jake B.
Jake B.

Reputation: 465

How to see why mysql_connect fails?

I've installed Linux (Mint) & LAMP but

mysql_connect('localhost', 'user', 'password') 

fails no matter what I try.

I spend at least 6 hours trying to fix it.

phpinfo()

is working

sudo mysql -u user -p

works

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
Apache Full                ALLOW       Anywhere                  
3306                       ALLOW       Anywhere                  
Apache Full (v6)           ALLOW       Anywhere (v6)             
3306 (v6)                  ALLOW       Anywhere (v6

What else can I try? How do I debug? I've been googling for hours. The only non-standard thing is that I've installed php 5.6, since I need it, "php -v" returns:

PHP 5.6.40-30+ubuntu20.04.1+deb.sury.org+1 (cli) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologie

Upvotes: 1

Views: 326

Answers (1)

Ron
Ron

Reputation: 6601

If you REALLY need to use mysql_connect and that old PHP:

The first example at https://www.php.net/mysql_connect shows how you can see the error... I am not sure where you spent those 6 hrs :)

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error()); // MAGIC LINE
}
echo 'Connected successfully';
mysql_close($link);
?>

My suggestion, as well as everyone else, is (as the comments under your question suggest): UPGRADE to PHP7.4 and then use the appropriate PDO/Mysqli connectors

Upvotes: 1

Related Questions