dpfwhb7470 2018-05-01 07:47
浏览 36
已采纳

表显示在phpmyadmin但不查询+更多

I'm still new to POSTING from an html web form with php to a database, but I think I've been able to piece together everything I need. But something is still missing and I don't know what.

I tested my connection and tables with:

<?php
  $link = mysqli_connect("localhost","#user","#password", "#database");
  if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
  }
  echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
  echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
  $test_query = "SHOW TABLES FROM" $#database;
  $result = mysqli_query($connect, $test_query);
  $tblCnt = 0;
  while($tbl = mysqli_fetch_array($result)) {
    $tblCnt++;
    #echo $tbl[0]."<br />
";}
    if (!$tblCnt) {
        echo "There are no tables<br />
";
    } else {
        echo "There are $tblCnt tables<br />
";
    }
  mysqli_close($link);
?>

I've tried 127.0.0.1 and an access host IP (RemoteMySQL) to no avail. My user has full permissions. I'm not aware of any other default security that could be hindering me.

I'm hosting with Bluehost, using MySQL and phpmyadmin. There are 2 tables in my database and they are found when I type "SHOW TABLES" in phpmyadmin. What am I missing? This is beyond my knowledge. I'm at my wits end...

Thank you so much!

  • 写回答

1条回答 默认 最新

  • douye5949 2018-05-01 09:18
    关注

    I see some problems in this line.

    $test_query = "SHOW TABLES FROM" $#database; // Line 10
    

    You don't have any variable named $database. And you have the symbol # that is not correct. # is used for comments ( http://php.net/manual/en/language.basic-syntax.comments.php )

    There is a space missing after FROM. This line should be

    $test_query = "SHOW TABLES FROM " . $database;
    

    Otherwise, the query generated would be "SHOW TABLES FROMDATABASE_NAME" which would trigger a SQL error.

    Also, you have a curly brace missing after the while loop. Here is the full code corrected.

    <?php
    $link = mysqli_connect("localhost","#user","#password", "#database");
    if (!$link) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
     }
     echo "Success: A proper connection to MySQL was made! The my_db 
     database is great." . PHP_EOL;
     echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
     $database = "YOUR_DATABASE_NAME_HERE";
     $test_query = "SHOW TABLES FROM " . $database;
     $result = mysqli_query($link, $test_query);
     $tblCnt = 0;
     while($tbl = mysqli_fetch_array($result)) {
         $tblCnt++;
     }
     // echo $tbl[0]."<br />
    ";}
     if (!$tblCnt) {
         echo "There are no tables<br />
    ";
     } else {
         echo "There are $tblCnt tables<br />
    ";
     }
     mysqli_close($link);
     ?>
    

    I hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)