php连接并显示mysql表 - LAMP服务器

Im running a LAMP server and im trying to show on the webpage the table of my database

This is my php script

$servername = "127.0.0.1";
$username = "root";
$password = "none";
$dbname = "chegada";

$conn = mysqli_connect($servername, $username, $password, $dbname);
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}else{echo "OK!";}

$sql = "SHOW TABLES FROM $dbname";
$result = mysql_query($sql);
if (!$result) {
    echo "DB Error, could not list tables
";
    echo 'MySQL Error: ' . mysql_error();
    exit;
}

And the webpage shows this

Warning: mysqli_connect(): (HY000/2002): Connection refused in /var/www/html/index.php on line 16

Notice: Trying to get property of non-object in /var/www/html/index.php on line 17

OK!

Fatal error: Uncaught Error: Call to undefined function mysql_query() in /var/www/html/index.php:22 Stack trace: #0 {main} thrown in /var/www/html/index.php on line 22

can someone help me out with this?

Try this

$servername = "127.0.0.1";
$username = "root";
$password = ""; // maybe this should be an empty string if you dont have password setup
$dbname = "chegada";

$conn = mysqli_connect($servername, $username, $password, $dbname); // this should be mysqli_query
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}else{echo "OK!";}

$sql = "SHOW TABLES FROM $dbname";
$result = mysqli_query($conn,$sql);
if (!$result) {
    echo "DB Error, could not list tables
";
    echo 'MySQL Error: ' . mysqli_error();
    exit;
} else {
    $res = mysqli_fetch_all($result);
    var_dump($res);
}

Use mysqli_query

$result = mysqli_query($conn,$sql);

I supect that you have a default local mysql, in which case password "none" is an empty string. Literally no password, not the word "none". You need:

$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "chegada";

Your second bug is caused by the fact you've started off in mysqli_ but then reverted to mysql_ half way down.