无法显示PDO mysql脚本中的数据,该脚本根据数据库结构自动生成表格布局

时间:2014-11-25 21:26:14

标签: php mysql pdo

我遇到的问题是下面的代码片段(整个页面的代码包含在下面),整个页面设计用于查看mysql数据库并给出所有表的列表(哪个有效) ,然后从下拉列表中选择一个表(工作正常),它将列出该表中的所有行,每个行都有一个标题行,每个列按照数据库中列的名称命名。

问题是下面的脚本根据表中的行数正确列出了行数但没有返回任何行数据,我尽力让数据显示在行中但是他们只是显得空白,有人可以告诉我做错了吗?

就像我正在注意的那样,我正在将脚本从在sqlite数据库上运行转换为mysql,只是在继续转换此脚本之前试图解决这个问题。

由于

<?
$alltables3=$db->query("SHOW COLUMNS FROM $table_name",PDO::FETCH_NUM);
$columns = array();
while($result3=$alltables3->fetch()){
$columns[] = $result3[0];
}
// Display * from SELECTED TABLE
$alltables4=$db->query("SELECT * FROM $table_name",PDO::FETCH_NUM);
while($result4=$alltables4->fetch()){

//echo $result4[0].'<br/>';

            echo "<tr>";
//          echo "<form name='delete' action='index.php?ID=$row[0]' method='post'>";
            $test = $result4[0];
        foreach ($columns as $col) 

            echo "<td>" . $row[$col] . "</td>";
            echo "<td><table cellspacing='0' cellpadding='0' style='border: 0px solid transparent'><tr><td><a id='alink' href='index.php?table=$table_name&ED=ED&ID=$test' class='button orange small'>Edit</a></td>";
            echo "<td><form action='' name='Delete' method='post'>";
            echo "<input type='hidden' name='table' value='$table_name' size='10'><input type='hidden' name='ID' value='$test' size='10'><input type='submit' name='Delete' class='button red small' value='Delete'>";
            echo "</form>";
            echo "</td></tr></table></td>";
    }
            echo "</tr>";

?>
            </tbody>

为了竞争,我在下面列出了整个页面的所有代码,其中包括上面的代码

<html>
<head>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
</head>
</body>
<?
   if($_SESSION['LOGGED_IN']=="YES")
   {
//----------------------------------------------------------------------------------------------------------------------------
// DO NOT EDIT ANYTHING BELOW THIS LINE
//----------------------------------------------------------------------------------------------------------------------------

// SET DEFAULT TABLE TO BE LISTED IN TABLES

// SET TABLE VARIABLE
   $table_name = $_GET['table'];
// DISPLAY ONLY THE FIRST TABLE

if (empty($_GET['table'])) {

    //$db = new SQLite3('../data.db');
      include('db.php');
/*
    $tablesquery = $db->query("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name LIMIT 1;");

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {
        if ($table['name'] != "sqlite_sequence") {
            $table_name = $table['name'];
            $_GET['table'] = $table['name'];
        }
    }
*/
} 

//----------------------------------------------------------------------------------------------------------------------------
?>
<body>
<br>
    <div id="tablewrapper">
        <div id="tableheader">
        <div class="search">
<form>

<select name='table' onchange='this.form.submit()'>
<option value="">All Tables</option>




<?php
// Display all sqlite table names
$alltables=$db->query("SHOW TABLES",PDO::FETCH_NUM);

while($result=$alltables->fetch()){

//echo $result[0].'<br/>';
//echo "<option value='".$result[0]."'>".$result[0]."</option>";


if ($result[0] == $_GET['table']) {

                            $table_name_2 = ucwords(strtolower(str_replace('_', ' ', $result[0])));
                        echo "<option selected='selected' value='".$result[0]."'>".$table_name_2."</option>";
                        }
                    else
                {
                            $table_name_2 = ucwords(strtolower(str_replace('_', ' ', $result[0])));
                        echo "<option value='".$result[0]."'>".$table_name_2."</option>";
                }


}
?>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>


            </div>
            <div class="search">
                <select id="columns" onchange="sorter.search('query')">

<?
$alltables=$db->query("SHOW COLUMNS FROM $table_name",PDO::FETCH_NUM);

while($result=$alltables->fetch()){

//echo $result[0].'<br/>';
//       echo "<th><h3>" . $result[0] . "</h3></th>";
//}
        $table_name_header = ucwords(strtolower(str_replace('_', ' ', $result[0])));
        //echo "<th><h3>" . $table_name_header . "</h3></th>";
        echo "<option value='".$result[0]."'>".$table_name_header."</option>";

}
?>

                </select>
                <input type="text" id="query" onkeyup="sorter.search('query')" />
            </div>
            <span class="details">
                <div>Records <span id="startrecord"></span>-<span id="endrecord"></span> of <span id="totalrecords"></span></div>
                <div><a href="javascript:sorter.reset()">Reset</a></div>
            </span>
        </div>
            <?
// DELETE ROW FROM DATABASE

if (isset($_POST['Delete'])) {

    $ID_delete = $_POST['ID'];

// INSERT VALUES INTO DATABASE
$db->exec('DELETE FROM ' . $table_name . ' WHERE ID = "' . $ID_delete . '"');
echo "Details have been Deleted";
echo "<br><br>";


} else { 
//echo "NO ID"; 
} 

?>
<?
// INSERT NEW ROW INTO DATABASE

if (isset($_POST['Add'])) {

// KEY DATA (TABLE COLUMN NAME)
$string_key = '';
    foreach ($_POST as $key => $value) {
    $string_key .= $key.',';
}
$string_key_final2 = str_replace(',table,Add,', '', $string_key);
$string_key_final = str_replace(',', ', ', $string_key_final2);

$string_value = '';

// KEY DATA (TABLE VARIABLES TO BE INSERTED)
    foreach ($_POST as $key => $value) {
    $string_value .= $value.',';
}

$string_value_1 = str_replace(',' . $table_name . ',Add,', '', $string_value);
$string_value_2 = "'" . str_replace("," , "', '", $string_value_1) . "'";
$string_value_final = str_replace('' , '', $string_value_2);

// INSERT VALUES INTO DATABASE
$db->exec('INSERT INTO ' . $table_name . '('. $string_key_final .') VALUES (' . $string_value_final . ')');

echo "Details have been Added";
echo "<br><br>";
}

?>


        <table cellpadding="0" cellspacing="0" border="0" id="table" class="tinytable">
            <thead>
                <tr>
<?php // Display all sqlite column names for chosen table

$alltables=$db->query("SHOW COLUMNS FROM $table_name",PDO::FETCH_NUM);

while($result=$alltables->fetch()){

        $table_name_header = ucwords(strtolower(str_replace('_', ' ', $result[0])));
        echo "<th><h3>" . $table_name_header . "</h3></th>";


}?>
<th><h3>Controls</h3></th>
                </tr>
            </thead>
            <tbody>


<?
$alltables3=$db->query("SHOW COLUMNS FROM $table_name",PDO::FETCH_NUM);
$columns = array();
while($result3=$alltables3->fetch()){
$columns[] = $result3[0];
}
// Display * from SELECTED TABLE
$alltables4=$db->query("SELECT * FROM $table_name",PDO::FETCH_NUM);
while($result4=$alltables4->fetch()){

//echo $result4[0].'<br/>';

            echo "<tr>";
//          echo "<form name='delete' action='index.php?ID=$row[0]' method='post'>";
            $test = $result4[0];
        foreach ($columns as $col) 

            echo "<td>" . $row[$col] . "</td>";
            echo "<td><table cellspacing='0' cellpadding='0' style='border: 0px solid transparent'><tr><td><a id='alink' href='index.php?table=$table_name&ED=ED&ID=$test' class='button orange small'>Edit</a></td>";
            echo "<td><form action='' name='Delete' method='post'>";
            echo "<input type='hidden' name='table' value='$table_name' size='10'><input type='hidden' name='ID' value='$test' size='10'><input type='submit' name='Delete' class='button red small' value='Delete'>";
            echo "</form>";
            echo "</td></tr></table></td>";
    }
            echo "</tr>";

?>
            </tbody>

        <form action="" method="post">
            <thead>
                <tr>



<?php // Display form details to add new row

    $tablesquery = $db->query("PRAGMA table_info($table_name)");

    while ($table = $tablesquery->fetchArray(SQLITE3_ASSOC)) {

//      $table_name_add = $table['name'];
//        echo "<th><h3>" . "<input type='text' name='$table_name_add' size='10'>" . "</h3></th>";

if ($table['name'] == "ID") {

        echo "<th><h3>&nbsp;</h3></th>";

} else {

            $table_name_add = $table['name'];
        echo "<th><h3>" . "<input type='text' name='$table_name_add' size='10'>" . "</h3></th>";

}        

} 
?>
<th><h3><input type='hidden' name='table' value='<? echo $table_name ?>' size='10'><input type="submit" name="Add" class="button green small" value="Add" />


</h3></th>
<!--
<input type="image" src="images/login.jpg" alt="Submit Form" /> 
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_paynowCC_LG.gif" border="0" name="Submit" alt="PayPal - The safer, easier way to pay online!">
-->

                </tr>
            </thead>
        </form>            


        </table>
        <div id="tablefooter">
          <div id="tablenav">
                <div>
                    <img src="images/first.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1,true)" />
                    <img src="images/previous.gif" width="16" height="16" alt="First Page" onclick="sorter.move(-1)" />
                    <img src="images/next.gif" width="16" height="16" alt="First Page" onclick="sorter.move(1)" />
                    <img src="images/last.gif" width="16" height="16" alt="Last Page" onclick="sorter.move(1,true)" />
                </div>
                <div>
                    <select id="pagedropdown"></select>
                </div>
                <div>
                    <a href="javascript:sorter.showall()">view all</a>
                </div>
            </div>
            <div id="tablelocation">
                <div>
                    <select onchange="sorter.size(this.value)">
                    <option value="5">5</option>
                        <option value="10" selected="selected">10</option>
                        <option value="20">20</option>
                        <option value="50">50</option>
                        <option value="100">100</option>
                    </select>
                    <span>Entries Per Page</span>
                </div>
                <div class="page">Page <span id="currentpage"></span> of <span id="totalpages"></span></div>
            </div>
        </div>
    </div>
    <script type="text/javascript" src="script.js"></script>
    <script type="text/javascript">
    var sorter = new TINY.table.sorter('sorter','table',{
        headclass:'head',
        ascclass:'asc',
        descclass:'desc',
        evenclass:'evenrow',
        oddclass:'oddrow',
        evenselclass:'evenselected',
        oddselclass:'oddselected',
        paginate:true,
        size:10,
        colddid:'columns',
        currentid:'currentpage',
        totalid:'totalpages',
        startingrecid:'startrecord',
        endingrecid:'endrecord',
        totalrecid:'totalrecords',
        hoverid:'selectedrow',
        pageddid:'pagedropdown',
        navid:'tablenav',
        sortcolumn:0,
<?
/*
        sortdir:1,
        sum:[8],
        avg:[6,7,8,9],
        columns:[{index:7, format:'%', decimals:1},{index:8, format:'$', decimals:0}],
*/
?>
        init:true
    });
  </script>

<? include 'footer.php'; ?>
</body>
</html>
<?
}
?>

1 个答案:

答案 0 :(得分:0)

您正在获取一个数字键控数组:

$alltables4=$db->query("SELECT * FROM $table_name",PDO::FETCH_NUM);
                                                   ^^^^^^^^^^^^^^

但是你在显示代码中使用了列名:

        echo "<td>" . $row[$col] . "</td>";
                           ^^^^

e.g。你正在做echo $row['id']而不是echo $row[0]。或者获取一个关联数组,这样你就可以按原样$row[$col]