无法在网页中显示来自mysql的两个数据库

时间:2018-08-12 05:54:35

标签: php mysql

我无法在网页中显示我的两个数据库。我有一个数据库,用户可以在其中创建一个称为“票证”的新“票证”,当该票证标记为“待处理”时,它将数据库条目移至一个名为“ out_tickets”的新数据库表中。因此,在我的index.php页面上,我像仪表板视图一样显示新的“票证”以及显示“待处理”票证。但是现在我正在努力在index.php上显示数据库中的“待处理”票证结果。

这是我的代码:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<style type="text/css">
    .wrapper{
        width: 0 auto;
        margin: 0 auto;
    }
    .page-header h2{
        margin-top: 0;
    }
    table tr td:last-child a{
        margin-right: 15px;
    }
</style>
<script type="text/javascript">
    $(document).ready(function(){
        $('[data-toggle="tooltip"]').tooltip();   
    });
</script>
</p>
<body>
<div class="wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">

                </div>
                <?php
                // Include config file
                require_once 'config.php';

                // Attempt select query execution
                $sql = "SELECT * FROM tickets";
                if($result = mysqli_query($link, $sql)){
                    if(mysqli_num_rows($result) > 0){
                        echo "<table class='table table-bordered table-striped'>";
                            echo "<thead>";
                                echo "<tr>";
                                    echo "<th>#</th>";
                                    echo "<th>Name</th>";
                                    echo "<th>Address</th>";
                                    echo "<th>Contact Details</th>";
                                    echo "<th>Email</th>";
                                    echo "<th>Job Type</th>";
                                    echo "<th>Description</th>";
                                    echo "<th>Action</th>";
                                echo "</tr>";
                            echo "</thead>";
                            echo "<tbody>";
                            while($row = mysqli_fetch_array($result)){
                                echo "<tr>";
                                    echo "<td>" . $row['id'] . "</td>";
                                    echo "<td>" . $row['client_name'] . "</td>";
                                    echo "<td>" . $row['client_address'] . "</td>";
                                    echo "<td>" . $row['client_contact'] . "</td>";
                                    echo "<td>" . $row['client_email'] . "</td>";
                                    echo "<td>" . $row['client_jobtype'] . "</td>";
                                    echo "<td>" . $row['client_description'] . "</td>";
                                    echo "<td>";
                                        echo "<a href='read.php?id=". $row['id'] ."' title='View Task' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
                                        echo "<a href='update.php?id=". $row['id'] ."' title='Update Task' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
                                        echo "<a href='delete.php?id=". $row['id'] ."' title='Delete Task' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
                                        echo "<a href='pending.php?id=". $row['id'] ."' title='Task Pending' data-toggle='tooltip'><span class='glyphicon glyphicon-time'></span></a>";
                                        echo "<a href='complete.php?id=". $row['id'] ."' title='Mark Complete' data-toggle='tooltip'><span class='glyphicon glyphicon-ok'></span></a>";

                                    echo "</td>";
                                echo "</tr>";
                            }
                            echo "</tbody>";                            
                        echo "</table>";
                        // Free result set
                        mysqli_free_result($result);
                    } else{
                        echo "<p class='lead'><em>No records were found.</em></p>";
                    }
                } else{
                    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                }

                // Close connection
                mysqli_close($link);
                ?>
            </div>
        </div>        
    </div>
</div>
<h1 style="font-size:25px;
           background-color:darkgray;
           border:2px solid black;
           text-align:center">
    Outstanding Tasks
</h1>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<style type="text/css">
    .wrapper{
        width: 0 auto;
        margin: 0 auto;
    }
    .page-header h2{
        margin-top: 0;
    }
    table tr td:last-child a{
        margin-right: 15px;
    }
</style>
<script type="text/javascript">
    $(document).ready(function(){
        $('[data-toggle="tooltip"]').tooltip();   
    });
</script>
</p>
<body>
<div class="wrapper">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12">

                </div>
                <?php
                require_once 'config.php';
                // Attempt select query execution
                $sql = "SELECT * FROM out_tickets";
                if($result = mysqli_query($link, $sql)){
                    if(mysqli_num_rows($result) > 0){
                        echo "<table class='table table-bordered table-striped'>";
                            echo "<thead>";
                                echo "<tr>";
                                    echo "<th>#</th>";
                                    echo "<th>Name</th>";
                                    echo "<th>Address</th>";
                                    echo "<th>Contact Details</th>";
                                    echo "<th>Email</th>";
                                    echo "<th>Job Type</th>";
                                    echo "<th>Description</th>";
                                    echo "<th>Action</th>";
                                echo "</tr>";
                            echo "</thead>";
                            echo "<tbody>";
                            while($row = mysqli_fetch_array($result)){
                                echo "<tr>";
                                    echo "<td>" . $row['id'] . "</td>";
                                    echo "<td>" . $row['client_name'] . "</td>";
                                    echo "<td>" . $row['client_address'] . "</td>";
                                    echo "<td>" . $row['client_contact'] . "</td>";
                                    echo "<td>" . $row['client_email'] . "</td>";
                                    echo "<td>" . $row['client_jobtype'] . "</td>";
                                    echo "<td>" . $row['client_description'] . "</td>";
                                    echo "<td>";
                                        echo "<a href='read.php?id=". $row['id'] ."' title='View Task' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
                                        echo "<a href='update.php?id=". $row['id'] ."' title='Update Task' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
                                        echo "<a href='delete.php?id=". $row['id'] ."' title='Delete Task' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
                                        echo "<a href='pending.php?id=". $row['id'] ."' title='Task Pending' data-toggle='tooltip'><span class='glyphicon glyphicon-time'></span></a>";
                                        echo "<a href='complete.php?id=". $row['id'] ."' title='Mark Complete' data-toggle='tooltip'><span class='glyphicon glyphicon-ok'></span></a>";

                                    echo "</td>";
                                echo "</tr>";
                            }
                            echo "</tbody>";                            
                        echo "</table>";
                        // Free result set
                        mysqli_free_result($result);
                    } else{
                        echo "<p class='lead'><em>No records were found. </em></p>";
                    }
                } else{
                    echo "ERROR: Could not able to execute $sql. " . 
 mysqli_error($link);
                }

                // Close connection
                mysqli_close($link);
                ?>
            </div>
        </div>        
    </div>
</div>

如您所见,第一个$sql = "SELECT * FROM tickets"; 可以,但是这个不$sql = "SELECT * FROM out_tickets";

我在MySQL中有这两个数据库,可以从PHPmyAdmin查看它们中的数据

这是我得到的错误:

Warning: mysqli_query(): Couldn't fetch mysqli in 
C:\index.php on line 211

Warning: mysqli_error(): Couldn't fetch mysqli in 
C:\index.php on line 254

ERROR: Could not able to execute SELECT * FROM out_tickets. 

Warning: mysqli_close(): Couldn't fetch mysqli in 
C:\index.php on line 258

2 个答案:

答案 0 :(得分:1)

我认为您的config.php仅连接到第一个数据库。您可以简单地在另一个数据库中设置另一个连接字符串,将其命名为$ link_2并在从out_tickets查询时使用$ link_2

答案 1 :(得分:0)

您介绍了两个带有特定表的数据库。从您的代码中,我可以看到SELECT * FROM out_tickets等于require_once 'config.php';行。您必须连接到另一个数据库(根据您的判断)。