繁体   English   中英

使用循环获取数据库中的所有表

[英]Using a loop to get all tables within a database

请理解,我对PhP还是陌生的,我可能是不知道答案就回答了我的问题。 如果是这种情况,请指示我正确的答案。

我有一个包含多个表的数据库。 每个表的确具有完全相同的结构,因为每个表对应于一家餐厅连锁店中的不同单位。 我正在尝试为特许经营所有者创建一个“母版”页面,以便能够将所有(或一个)表上拉到一页上,以便查看与每个单位相关的数据。 为了使代码正常工作,我尝试了所有可能想到的方法,但是我不断遇到错误。 我收到的最新错误消息是“您的SQL语法有错误;请查看与您的MariaDB服务器版本相对应的手册,以找到在第1行的''附近使用的正确语法”

在过去的问题中,人们试图告诉我,我的代码必须是一种方式,因为我只包含部分代码。 这次,我将在引发错误的页面中包含所有代码。

<?php
require_once('../dbconnection.php');

$tables = array($_POST["spenard"], $_POST["benson"], $_POST["eagle"],
$_POST["wasilla"], $_POST["boniface"], $_POST["dimond"], $_POST["tudor"],
$_POST["huffman"], $_POST["dtown"], $_POST["airport"], $_POST["college"],
$_POST["npole"], $_POST["kenai"], $_POST["suldotna"]);
$length = count($tables);


?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Food Safety Checklist Master List</title>

<script>

</script>
</head>

<body>

<!--<table>
    ?php
        for ($i = 0; $i < $length; $i++) {?>
        <tr>
            <th>?php echo $tables[$i]; ?></th>
        </tr>
        ?php }
    ?>
    <tr>

    </tr>
</table>-->

<table>
    <tr>
        <th>Food Safety Checklist</th>
    </tr>
    <?php for ($i = 0; $i < $length; $i++) { 
        mysqli_select_db($conn, $dbname);
        $query_lists = "SELECT * FROM $tables[$i]";
        $lists = mysqli_query($conn, $query_lists) or
die(mysqli_error($conn));
        $row_lists = mysqli_fetch_assoc($lists);
        $totalRows_lists = mysqli_num_rows($lists);?>
    <tr>
        <th><?php echo $row_lists[' ']; ?></th>
    </tr>
    <?php } ?>
</table>

</body>
</html>

启动页面的代码是:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Food Safety Checklist Master Selection</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/additional-methods.min.js"></script>
</head>

<body>

<header>
    <h1>Pizza Hut Food Safety</h1>
</header>

<section>
    <div>
        <form action="mastershow.php" method="post" name="masterselect" id="masterselect">
            <table>
                <tr>
                    <th colspan="2"><h2>Stores</h2></th>
                </tr>
                <tr>
                    <th colspan="2"><input type="submit" value="Run" id="submit" name="submit"></th>
                </tr>
                <tr>
                    <td><label for="spenard">Spenard 010576</label></td>
                    <td><input name="spenard" id="spenard" type="checkbox" value="`010576`"><br/></td>
                </tr>
                <tr>                
                    <td><label for="benson">Benson 011169</label></td>
                    <td><input name="benson" id="benson" type="checkbox" value="`011169`"><br/></td>
                </tr>
                <tr>
                    <td><label for="eagle">Eagle River 008181</label></td>
                    <td><input name="eagle" id="eagle" type="checkbox" value="`008181`"><br/></td>
                </tr>
                <tr>
                    <td><label for="wasilla">Wasilla 010324</label></td>
                    <td><input name="wasilla" id="wasilla" type="checkbox" value="`010324`"><br/></td>
                </tr>
                <tr>
                    <td><label for="boniface">Boniface 008615</label></td>
                    <td><input name="boniface" id="boniface" type="checkbox" value="`008615`"><br/></td>
                </tr>
                <tr>
                    <td><label for="dimond">W. Dimond 009150</label></td>
                    <td><input name="dimond" id="dimond" type="checkbox" value="`009150`"><br/></td>
                </tr>
                <tr>
                    <td><label for="tudor">Tudor 014640</label></td>
                    <td><input name="tudor" id="tudor" type="checkbox" value="`014640`"><br/></td>
                </tr>
                <tr>
                    <td><label for="huffman">Huffman 0101684</label></td>
                    <td><input name="huffman" id="huffma" type="checkbox" value="`010684`"><br/></td>
                </tr>
                <tr>
                    <td><label for="dtown">6TH AVE 011168</label></td>
                    <td><input name="dtown" id="dtwon" type="checkbox" value="`011168`"><br/></td>
                </tr>
                <tr>
                    <td><label for="airport">Airport Rd. 014215</label></td>
                    <td><input name="airport" id="airport" type="checkbox" value="`014215`"><br/></td>
                </tr>
                <tr>
                    <td><label for="college">College Rd. 008179</label></td>
                    <td><input name="college" id="college" type="checkbox" value="`008179`"><br/></td>
                </tr>
                <tr>
                    <td><label for="npole">North Pole 008339</label></td>
                    <td><input name="npole" id="npole" type="checkbox" value="`008339`"><br/></td>
                </tr>
                <tr>
                    <td><label for="kenai">Kenai 008668</label></td>
                    <td><input name="kenai" id="kenai" type="checkbox" value="`008668`"><br/></td>
                </tr>
                <tr>
                    <td><label for="suldotna">Suldotna 031574</label></td>
                    <td><input name="suldotna" id="suldotna" type="checkbox" value="`031574`"><br/></td>
                </tr>
            </table>
        </form>
    </div>
</section>

<footer>
    Designed by Greg Weiland &copy;2017
</footer>   

</body>
</html>

再次,我试图让特许经营权所有者可以在其中“检查”要运行报告的存储,然后能够从对应的表中获取所有行(同样,每个表的名称对应于其中一个他选择的单位编号)。 每个表具有相同的结构。 感谢您提供的所有帮助

它告诉您您的查询是错误的,它是您内部有一个变量,却没有告诉它这是一个变量。

$query_lists = "SELECT * FROM '$tables[$i]'";

编辑由于类型数组而更改

  //see my last comment on why i do it this way
  <?php foreach ($tables as $qtables) { 
            mysqli_select_db($conn, $dbname);
            $query_lists = "SELECT * FROM '$qtables'";

试试这个代替:

$sql = "SELECT * FROM ".$tables[$i];
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"] . "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();

然后,例如,您在选择的内容中寻找ID,然后使用$ results ['id']进行查找;

还有一个小注:我喜欢将数据库查询方法放在我导入的单独的php文件中。 在这种情况下,我将所有数据收集到$ results数组中并返回该数组。

像这样的东西:

$sql = "SELECT * FROM ".$tables[$i];
$result = $conn->query($sql);
$results = array();  
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $results[] = $row;
    }
} else {
    echo "0 results";
}
$conn->close();
return $results;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM