Php/Mysql multi queries on same file -
beginner php / mysql here. have file clientdetails.php, uses method connect mysql database , retrieve data (top half of code below). on same file, have bootstrap tabs. on 1 of these tabs, run mysql query obtain different data same database.
the error getting is:
warning: mysqli::query(): couldn't fetch mysqli in c:\wamp64\www\crud\clientdetails.php on line 51 i suspect has connection existing?
this simplified version of 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 select statement $sql = "select * client client_id = ?"; if($stmt = $mysqli->prepare($sql)){ // bind variables prepared statement parameters $stmt->bind_param("i", $param_id); // set parameters $param_id = trim($_get["client_id"]); // attempt execute prepared statement if($stmt->execute()){ $result = $stmt->get_result(); if($result->num_rows == 1){ /* fetch result row associative array. since result set contains 1 row, don't need use while loop */ $row = $result->fetch_array(mysqli_assoc); // retrieve individual field value } else{ // url doesn't contain valid id parameter. redirect error page header("location: error.php"); exit(); } } else{ echo "oops! went wrong. please try again later."; } } // close statement $stmt->close(); // close connection $mysqli->close(); } else{ // url doesn't contain id parameter. redirect 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 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 found.</em></p>"; } } else{ echo "error: not able execute $sql. " . $mysqli->error; } // close connection $mysqli->close(); ?> </div> </div> </div> config file:
<?php /* database credentials. assuming running mysql server default setting (user 'root' no password) */ define('db_server', 'localhost'); define('db_username', 'root'); define('db_password', 'mypassword'); define('db_name', 'mydatabase'); /* attempt connect mysql database */ $mysqli = new mysqli(db_server, db_username, db_password, db_name); // check connection if($mysqli === false){ die("error: not connect. " . $mysqli->connect_error); } ?>
the problem is:
- you did require_once config.php, means, included once (in first condition)
- you did close mysqli connection on first condition, mysqli not accessible anymore after that.
you should not close connection in first condition, can reuse in second condition (tabs). practice close connection on end of file.
Comments
Post a Comment