2015-11-22 62 views
0

我有一個函數名爲record_list()這可以幫助我呼應從數據庫提取的查詢每次刷新/訪問頁面時。我試圖通過兩次回顯來使用此函數,但不幸的是,腳本隱藏後我所有的DOM元素都被隱藏了,而且我無法在HTML中使用以下分區/列表。但是如果我一旦它沒有任何問題地工作就可以打電話。錯誤調用相同的函數兩次在php

除了形成這個我收到此錯誤:

Fatal error: Call to a member function query() on a non-object

record_list():

function record_list(){ 
//include db configuration file 
include_once("configuration.php"); 
try{ 
//MySQL query 
$user_id = $_SESSION['user_id']; 
$results = $mysqli->query("SELECT * FROM education_record WHERE user_id='$user_id' "); 
//get all records from add_delete_record table 
while($row = $results->fetch_assoc()) 
    { 
    echo '<li class = "col-md-4 col-xs-12 records_display" id="item_'.$row["id"].'">'; 
    echo '<div class="del_wrapper"><a href="#" class="del_btn" id="del-'.$row["id"].'">'; 
    echo '<img src="../img/removeButtonIcon.svg" height ="20px" width ="20px" border="0" />'; 
    echo '</a></div>'; 
    echo '<div class="controls group_row">'; 
    echo '<div class="controls group">'; 
    echo  '<input disabled type="text"class="group"style="width:175px" value ="'.$row["degree_name"].'"/>'; 
    echo  '<input disabled type="text"class="group"style="width:175px" value ="'.$row["institute"].'"/>'; 
    echo  '<input disabled type="text"class="group"style="width:175px" value ="'.$row["specialisation"].'"/>'; 
    echo  '<input disabled type="text"class="group"style="width:100px" value ="'.$row["date_of_passing"].'"/>'; 
    echo '</div>'; 
    echo '</div>'; 
    echo '</li>'; 
    } 
} 
catch (mysqli_sql_exception $e) { 
    throw $e; 
    die(); 
} 
$mysqli->close(); 
//close db connection 


} 

的configuration.php:

<?php 
$host = 'localhost'; 
$dbname = 'databasename'; 
$username = 'username'; 
$password = 'can-be-anything'; 


try { 
$mysqli = new mysqli($host, $username, $password, $dbname); 
} catch (mysqli_sql_exception $e) { 
throw $e; 
die(); 
} 
?> 

請幫我鑑定這個錯誤。

+2

這是因爲你必須使用include(「configuration.php」);'not'include_once(「configuration.php」);'如果你使用'include_once',它只會在這個配置文件的第一個實例包含在腳本的某處。 – Rasclatt

+0

@Rasclatt是對的。如果你把'include'放在你的函數之外,那也更好。當你聲明include_ONCE時,只有第一次工作正常。然後在第二次嘗試它違反了一次,使一團糟。 – sinaza

回答

1

這是因爲你必須使用include("configuration.php");include_once("configuration.php");如果你include_once,它只會當在腳本中包含此配置文件的第一個實例地方工作。

我會建議製作一個類來包裝連接,將它保存爲單例狀態,以便在腳本中的其他任何地方使用。下面是一個簡單的例子:

類/ class.DatabaseConfig.php

<?php 
class DatabaseConfig 
    { 
     private static $singleton; 

     public function __construct() 
      { 
       if(empty(self::$singleton)) 
        self::$singleton = $this; 

       return self::$singleton; 
      } 

     public function connect($host = "localhost", $username = "username", $password = "password", $database = "database") 
      { 
       // Create connection 
       try { 
         $mysqli = new mysqli($host, $username, $password, $database); 
         return $mysqli; 
        } catch (mysqli_sql_exception $e) { 
         throw $e; 
         die(); 
        } 
      } 
    } 

類/ class.Db.php

<?php 
class Db 
    { 
     private static $singleton; 
     public static function mysqli() 
      { 
       if(empty(self::$singleton)) { 
        $con = new DatabaseConfig(); 
        self::$singleton = $con->connect(); 
       } 

       return self::$singleton; 
      } 
    } 

的index.php

<?php 
// Look into using spl_autoload_register() here 
include_once("classes/class.DatabaseConfig.php"); 
include_once("classes/class.Db.php"); 
// This will work 
$con = Db::mysqli(); 

function myQuery() 
    { 
     // This will also work, so long as you have included the class 
     // file once before this function (not necessarily in this function either) 
     // is called to use 
     $con = Db::mysqli(); 
    } 
0

發生這種情況的原因是,每次執行函數時都會關閉$mysqli->close();,但按照@ Rasclatt的建議,您只需使用include_once指令打開一次連接。你可以與包括更換include_once或嘗試@Rasclatt OOP的做法,但如果你不想潛入OOP由於某種原因,我建議連接操作從功能分離,並把它傳遞給函數作爲這樣

include_once("configuration.php"); 

function record_list($mysqli){ 
    try{ 
    //MySQL query 
     $user_id = $_SESSION['user_id']; 
     $results = $mysqli->query("SELECT * FROM education_record WHERE user_id='$user_id' "); 
     //get all records from add_delete_record table 
     while($row = $results->fetch_assoc()) 
     { 
      echo '<li class = "col-md-4 col-xs-12 records_display" id="item_'.$row["id"].'">'; 
      echo '<div class="del_wrapper"><a href="#" class="del_btn" id="del-'.$row["id"].'">'; 
      echo '<img src="../img/removeButtonIcon.svg" height ="20px" width ="20px" border="0" />'; 
      echo '</a></div>'; 
      echo '<div class="controls group_row">'; 
      echo '<div class="controls group">'; 
      echo  '<input disabled  type="text"class="group"style="width:175px" value ="'.$row["degree_name"].'"/>'; 
      echo  '<input disabled type="text"class="group"style="width:175px" value ="'.$row["institute"].'"/>'; 
      echo  '<input disabled type="text"class="group"style="width:175px" value ="'.$row["specialisation"].'"/>'; 
      echo  '<input disabled type="text"class="group"style="width:100px" value ="'.$row["date_of_passing"].'"/>'; 
      echo '</div>'; 
      echo '</div>'; 
      echo '</li>'; 
     } 
    } 
    catch (mysqli_sql_exception $e) { 
     throw $e; 
     die(); 
    } 

} 

/// You may call here record_list($mysqli) function as many times as you wish 
record_list($mysqli) 

$mysqli->close(); 
//close db connection 
參數
+0

我試圖這樣做。將include語句放在函數體中。但我得到同樣的錯誤。同樣定義一個需要相同配置文件的函數會顯示錯誤。 :( – cRAN