2017-07-28 9 views
-1

初心者のPHP/Mysqlはこちら。 私はファイルclientdetails.phpを持っています。これは、GETメソッドを使用してMysqlデータベースに接続し、データを取得します(下のコードの上半分)。同じファイルには、ブートストラップタブがあります。これらのタブの1つでは、同じデータベースから別のデータを取得するために別のMySQLクエリを実行したいと思います。同じファイルのPhp/Mysqlマルチクエリ

私は取得していますエラーは次のとおりです。

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

私は、これはすでに既存の接続とは何かを持っている疑いがありますか?

<?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); 
} 
?> 
+0

show ur config.php –

+0

これが行われました – wazzahenry

+0

接続を閉じないでください。 – WheatBeak

答えて

3

問題がある:

  • あなたでした(最初の状態で)、それは一度だけ含まれることを意味しますrequire_onceのconfig.phpを、
  • 最初の条件でmysqli接続を終了したので、その後mysqliにアクセスできなくなりました。

2番目の条件(タブ)で再利用できるように、最初の条件で接続を閉じるべきではありません。良い方法は、ファイルの最後に接続を閉じることです。

2

あなたはそれに価値を与える前に、パラメータをバインドしている

これはclientdetails.phpの簡易版です。私はそれが厳しい何かを修正するかどうか全く考えていません。

// 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); 
+0

修正されました – wazzahenry

関連する問題