douzhuang2570 2016-06-08 10:42
浏览 48

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?

  • 写回答

3条回答 默认 最新

  • douhu3424 2016-06-08 10:46
    关注

    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);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭