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.

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

报告相同问题?