在php中调用两次相同函数时出错

时间:2015-11-22 07:12:16

标签: php sql

我有一个名为record_list()的函数,每次刷新/访问页面时,它都可以帮助我回显数据库中的读取查询。我尝试通过回显两次来使用此函数,但不幸的是,脚本之后的所有DOM元素都被隐藏了,我不能在HTML中使用以下的分区/列表。但如果我把它称之为它没有任何问题就可以正常工作。

除此之外,我收到此错误:

  

致命错误:在非对象上调用成员函数query()

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 个答案:

答案 0 :(得分: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();
    }

答案 1 :(得分:0)

这是因为每次执行函数时都会关闭与$mysqli->close();的连接,但正如@Rasclatt建议的那样,只使用include_once指令打开一次连接。您可以用include替换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