对同一文件的Php / Mysql多查询

时间:2017-07-28 14:01:54

标签: php mysql twitter-bootstrap

初学者PHP / Mysql在这里。 我有一个文件 clientdetails.php ,它使用GET方法连接到Mysql数据库并检索数据(下面的代码的上半部分)。在同一个文件中,我有引导选项卡。在其中一个选项卡上,我想运行另一个Mysql查询以从同一个数据库中获取不同的数据。

我得到的错误是:

Warning: mysqli::query(): Couldn't fetch mysqli in C:\wamp64\www\crud\clientdetails.php on line 51

我怀疑这与已经存在的连接有关?

这是clientdetails.php的简化版本:

<?php
    // Check existence of id parameter before processing further
    if(isset($_GET["client_id"]) && !empty(trim($_GET["client_id"]))){
        // Include config file
        require_once 'config.php';
        // Prepare a select statement
        $sql = "SELECT * FROM client WHERE client_id = ?";
        if($stmt = $mysqli->prepare($sql)){
            // Bind variables to the prepared statement as parameters
            $stmt->bind_param("i", $param_id);
            // Set parameters
            $param_id = trim($_GET["client_id"]);
           // Attempt to execute the prepared statement
            if($stmt->execute()){
                $result = $stmt->get_result();
                if($result->num_rows == 1){
                    /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
                    $row = $result->fetch_array(MYSQLI_ASSOC);
                    // Retrieve individual field value
                } else{
                    // URL doesn't contain valid id parameter. Redirect to error page
                    header("location: error.php");
                    exit();
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
        // Close statement
        $stmt->close();
        // Close connection
        $mysqli->close();
    } else{
        // URL doesn't contain id parameter. Redirect to error page
        header("location: error.php");
        exit();
    }
?>

 <ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#sectionA">Details</a></li>
</ul>
    <div class="tab-content">
    <div id="Account" class="tab-pane fade">
        <div class="form-group">
        <?php
                // Include config file
        require_once 'config.php';
        // Attempt select query execution
        $sql = "SELECT transaction FROM client";
        if($result = $mysqli->query($sql)){    
            if($result->num_rows > 0){
                echo "<table class='table table-bordered table-striped'>";
                    echo "<thead>";
                        echo "<tr>";
                            echo "<th>#</th>";
                         echo "</tr>";
                    echo "</thead>";
                echo "<tbody>";
                while($row = $result->fetch_array()){
                        echo "<tr>";
                            echo "<td>" . $row['client_id'] . "</td>";
                            echo "</td>";
                        echo "</tr>";
                    }
                    echo "</tbody>";
                echo "</table>";
                $result2->free();
            } else{
                echo "<p class='lead'><em>No records were found.</em></p>";
            }
        } else{
            echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
        }
        // Close connection
        $mysqli->close();
        ?>
        </div>
    </div>
    </div>

配置文件:

<?php
/* Database credentials. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'mypassword');
define('DB_NAME', 'mydatabase');
/* Attempt to connect to MySQL database */
$mysqli = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check connection
if($mysqli === false){
    die("ERROR: Could not connect. " . $mysqli->connect_error);
}
?>

2 个答案:

答案 0 :(得分:3)

问题是:

  • 你做了require_once config.php,这意味着它只会被包含一次(在第一个条件下)
  • 您在第一个条件下关闭了mysqli连接,因此之后不再可以访问mysqli。

您不应在第一个条件中关闭连接,因此您可以在第二个条件(标签)中重复使用它。好的做法是关闭文件末尾的连接。

答案 1 :(得分:2)

在为参数赋值之前绑定参数。我完全不知道是否能解决这个问题。

// Bind variables to the prepared statement as parameters
$stmt->bind_param("i", $param_id);
// Set parameters
$param_id = trim($_GET["client_id"]);

应该是

// Set parameters
$param_id = trim($_GET["client_id"]);
// Bind variables to the prepared statement as parameters
$stmt->bind_param("i", $param_id);