dqd82461
dqd82461
2016-03-09 02:17

从数据库获取表名并将其输出到html表以供下载

已采纳

I am attempting to get data from a mysql database. This is the code that I have so far:

include 'sqlConnection.php';
$strSQL = "SELECT * FROM PrimeSelectScan";
$rs = $sqlTableConnection->query($strSQL);
if ($sqlTableConnection->query($rs) === TRUE) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . $sqlTableConnection->error;
} 
while($row = mysqli_fetch_array($rs)) 
{
    echo "\t<tr><td>".$row['id']."</td><td>".$row['cost']."</td><td>".$row['roi']."</td></tr>
";
}
$sqlTableConnection->close();

The sqlConnection.php file is just the connection to the database. I am not exactly sure if I am correct or not. If someone could help me out and lead me in the right direction, I would really appreciate it. This is the first time I have attempted to get information like this. Thanks in advance.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doucanshou6998 doucanshou6998 5年前

    Are you getting any error messages?

    Here is an example for a mysqli query:

    $mysqli = new mysqli("localhost", "user", "pass", "db");
    $query  = "SHOW TABLES FROM primeselectscan";
    $result = $mysqli->query($query);
    
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=FILE-NAME-HERE.txt");
    header("Pragma: no-cache");
    header("Expires: 0");
    
    while($row = $result->fetch_array()) {
        echo "\t<tr><td>".$row['Tables_in_primeselectscan']."</td><td>
    ";
    }
    
    $mysqli->close();
    

    If you are wanting the actual table names within the database then you need to change your query to use 'SHOW TABLES FROM dbName'. See http://dev.mysql.com/doc/refman/5.7/en/show-tables.html for details.

    To have it output as an actual download add the following headers before you output anything, I added that to the code section above.

    点赞 评论 复制链接分享
  • duangangmo0583 duangangmo0583 5年前

    First of all your heading says get table names for output so I am answering that. However, it doesn't look like that's what you're trying to do in your code

    .... If getting table names IS what you want.. You can use

    $sql = "SHOW TABLES FROM $dbname";

    Then loop through:

    while ($row = mysql_fetch_row($result)) {
        echo "<a> tag and <tr><td> stuff" . $row[0] . "end </td></tr> stuff";
    }
    
    点赞 评论 复制链接分享