结合2个php sql查询

时间:2017-11-30 19:17:19

标签: php mysql

嗨,有人可以帮我把这两个代码放在一起。

我是PHP的新手,我花了4个小时才得到一个代码才能工作,但现在我需要将相同的两个代码组合起来,只需调用另一个表。但无论我做什么,它都不会使输出只输出第一个输出。我得到的错误的第二个代码是:

  

“错误:无法执行SELECT * FROM treasure。”

我认为这是因为我在第一个代码中关闭了连接,所以我尝试删除了关闭,但这没有帮助

这是我的代码:

<?php
if($instance=="1")
{
    $sql = "SELECT * FROM treasure";
    if($result = mysqli_query($link, $sql))
    {
        if(mysqli_num_rows($result) > 0)
            {
                echo "<table id='Cheader'>";
                echo "<tr>";
                echo "<th>Day</th>";
                echo "<th>Green Fee</th>";
                echo "<th>Cart</th>";
                echo "<th>Caddy</th>";
                echo "<th>Visitor Green Fee</th>";
                echo "<th>Note</th>";
                echo "</tr>";
                while($row = mysqli_fetch_array($result))
                    {
                        echo "<tr>";
                        echo "<td>" . $row['day'] . "</td>";
                        echo "<td>" . $row['cf'] . "</td>";
                        echo "<td>" . $row['cart'] . "</td>";
                        echo "<td>" . $row['caddy'] . "</td>";
                        echo "<td>" . $row['viscf'] . "</td>";
                        echo "<td>" . $row['note'] . "</td>";
                        echo "</tr>";
                    }
                    echo "</table>";
                    mysqli_free_result($result);
                } else
                    {
                        echo "No records matching your query were found.";
                    }
                } else
                    {
                        echo "ERROR: Could not able to execute $sql. " . 
                        mysqli_error($link);
                    }
                    mysqli_close($link);

                 }
if($instance=="2")
{
    $sql = "SELECT * FROM hill";
    if($result = mysqli_query($link, $sql))
    {
        if(mysqli_num_rows($result) > 0)
            {
                echo "<table id='Cheader'>";
                echo "<tr>";
                echo "<th>Day</th>";
                echo "<th>Green Fee</th>";
                echo "<th>Cart</th>";
                echo "<th>Caddy</th>";
                echo "<th>Visitor Green Fee</th>";
                echo "<th>Note</th>";
                echo "</tr>";
                while($row = mysqli_fetch_array($result))
                    {
                        echo "<tr>";
                        echo "<td>" . $row['day'] . "</td>";
                        echo "<td>" . $row['cf'] . "</td>";
                        echo "<td>" . $row['cart'] . "</td>";
                        echo "<td>" . $row['caddy'] . "</td>";
                        echo "<td>" . $row['viscf'] . "</td>";
                        echo "<td>" . $row['note'] . "</td>";
                        echo "</tr>";
                    }
                    echo "</table>";
                    mysqli_free_result($result);
                } else
                    {
                        echo "No records matching your query were found.";
                    }
                } else
                    {
                        echo "ERROR: Could not able to execute $sql. " . 
                        mysqli_error($link);
                    }
                    mysqli_close($link);

                 }

?>  

0 个答案:

没有答案