1

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
Shadow
  • 33,525
  • 10
  • 51
  • 64
Jake B.
  • 435
  • 3
  • 13
  • 2
    Fails? What's the error? How does it fail? Could start with what is returned by `mysql_connect `. We assume `false`? – ficuscr Sep 29 '20 at 20:19
  • `php -v` in the command line is not necessarily the PHP on the webserver. What version of PHP is shown in `phpinfo()` in the browser? – ceejayoz Sep 29 '20 at 20:20
  • 5
    PHP5.6 is end-of-life since 2019, and the `mysql_*()` functions are removed from 7. Upgrade both your PHP version and your workflow. https://www.php.net/supported-versions.php https://www.php.net/manual/en/book.pdo.php – Sammitch Sep 29 '20 at 20:56
  • 1
    In 2020 do not even start with mysql_*() functions. If you have to support some legacy application, then it's fine, otherwise just forget about it. – Shadow Sep 29 '20 at 21:00
  • It fails because it is very, very old. Just like granny trying to juggle glasses of hot milk – Martin Sep 29 '20 at 21:03
  • If you are still using PHP 5 I strongly recommend to upgrade as soon as possible. This version is no longer supported. [Let Rasmus Lerdorf explain it to you](https://youtu.be/wCZ5TJCBWMg?t=2434) – Dharman Sep 29 '20 at 21:12
  • I understand PHP 5 is old. I had a Moodle server that worked on 5.6 and the HDD stopped working and I am only left with database dumps. I have to restore the whole system to get data from the database (quizzes, grades, etc,...). After I get my data back, I will update the system to PHP 7. – Jake B. Sep 30 '20 at 05:44

1 Answers1

1

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

Ron
  • 5,900
  • 2
  • 20
  • 30
  • Thanks. I got "Could not connect: Server sent charset unknown to the client. Please, report to the developers". – Jake B. Sep 30 '20 at 05:41
  • https://stackoverflow.com/questions/51051440/server-sent-charset-255-unknown-to-the-client-set-mysql-charset-to-utf8-w-o – Ron Sep 30 '20 at 07:36
  • @JakeB. You're possibly connecting to a modern MySQL server. The problem with the legacy MySQL extension was that it wasn't being properly maintained so it just lacks basic features like proper Unicode support and, of course, it's wasn't being updated to include newer MySQL features. – Álvaro González Sep 30 '20 at 07:37
  • 1
    Yep. It will be way easier if he just upgrades his whole setup. – Ron Sep 30 '20 at 08:01