Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php/Mysql在同一文件上的多个查询_Php_Mysql_Twitter Bootstrap - Fatal编程技术网

Php/Mysql在同一文件上的多个查询

Php/Mysql在同一文件上的多个查询,php,mysql,twitter-bootstrap,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 我怀疑这与已经存

这里是初学者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);
}
?>

在给参数赋值之前,先绑定参数。我完全不知道这是否解决了任何棘手的问题

// 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);
问题是:

您确实需要_once config.php,这意味着在第一个条件中只包含一次 您确实在第一个条件下关闭了mysqli连接,因此此后mysqli将不再可访问。
您不应该在第一个条件中关闭连接,以便可以在第二个条件选项卡中重用它。好的做法是在文件末尾关闭连接。

show ur config.php此操作已完成可能不关闭连接?