Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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 未捕获错误:在bla中对null调用成员函数prepare()_Php_Mysql_Sql_Pdo - Fatal编程技术网

Php 未捕获错误:在bla中对null调用成员函数prepare()

Php 未捕获错误:在bla中对null调用成员函数prepare(),php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,我正在创建一个文件,将我的数据库列表显示到我的android应用程序中,但它使我不断出现以下错误: <br /> <b>Fatal error</b>: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\file_location\file_location\file_location\sql.php:40 Stack trace: #0 C:\xam

我正在创建一个文件,将我的数据库列表显示到我的android应用程序中,但它使我不断出现以下错误:

<br />
<b>Fatal error</b>:  Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\file_location\file_location\file_location\sql.php:40
Stack trace:
#0 C:\xampp\htdocs\android_p\function\getstudentlist.php(12): classes\server\sql-&gt;get_student_list()
#1 {main}
  thrown in <b>C:\xampp\htdocs\android_p\classes\server\sql.php</b> on line <b>40</b><br />

致命错误:未捕获错误:在C:\xampp\htdocs\file\u location\file\u location\file\u location\sql.php:40中调用null上的成员函数prepare() 堆栈跟踪: #0 C:\xampp\htdocs\android\u p\function\getstudentlist.php(12):classes\server\sql-get\u student\u list() #1{main} 在第40行的C:\xampp\htdocs\android\u p\classes\server\sql.php中抛出
我不知道我的错误发生在哪里这里是我的三个文件

此文件将以android的json格式保存数据
getstudentlist.php

<?php 
error_reporting( E_ALL );

include("../root/root.php");

use \classes\server\sql;

header("Content-Type: application/json");

$sql = new sql();

if($sql->get_student_list() != "")
{
    $list = $sql->get_student_list();
    echo "{\"students\":";
    echo json_encode($list);
    echo "}";
}
else
{
    echo "no data output!";
}

 ?>

此命令将执行所有sql命令
sql.php


最后,此文件将与服务器建立连接
config.php

<?php 
namespace classes\server;
use \PDO;

const db_host = "localhost";
const db_name = "dbname";
const db_username = "root";
const db_password = "";

class config
{
    private $connection = null;
    private $db_acct = array("db_access" => ["username" => db_username,
                                             "password" => db_password]);
    private $isConnected = null;
    private $connection_error = null;

    public function __construct()
    {
        try
        {
            $this->connection = new PDO("mysql:host=".db_host.";dbname=".db_name, $this->db_acct["db_access"]["username"], 
                                                                                  $this->db_acct["db_access"]["password"]);
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            if($this->connection)
            {
                $this->isConnected = $this->connection;
            }
            else
            {
                $this->isConnected = false;
            }       
        }
        catch(PDOException $x)
        {
            $this->connection_error = $x->getMessage();
        }

    }

    protected function connection()
    {
        return $this->isConnected;
    }

    protected function connection_error()
    {
        return $this->connection_error;
    }
}
 ?>

不需要显示我的root.php文件,因为该文件的任务是仅连接所有文件
希望你们能帮助我!
这对我来说意义重大。

获取学生列表()
的第一次调用将
$con
变量设置为
null
,因此在第二次调用期间出现错误消息。将
get\u student\u list()
的输出保存在一个变量中,然后随后使用该变量可能是值得的。。。或者更改DB类以不关闭连接。

fyi,您不需要执行所有连接/断开连接检查,它在脚本执行时结束,删除它,您的代码将像10行一样!
<?php 
namespace classes\server;
use \PDO;

const db_host = "localhost";
const db_name = "dbname";
const db_username = "root";
const db_password = "";

class config
{
    private $connection = null;
    private $db_acct = array("db_access" => ["username" => db_username,
                                             "password" => db_password]);
    private $isConnected = null;
    private $connection_error = null;

    public function __construct()
    {
        try
        {
            $this->connection = new PDO("mysql:host=".db_host.";dbname=".db_name, $this->db_acct["db_access"]["username"], 
                                                                                  $this->db_acct["db_access"]["password"]);
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            if($this->connection)
            {
                $this->isConnected = $this->connection;
            }
            else
            {
                $this->isConnected = false;
            }       
        }
        catch(PDOException $x)
        {
            $this->connection_error = $x->getMessage();
        }

    }

    protected function connection()
    {
        return $this->isConnected;
    }

    protected function connection_error()
    {
        return $this->connection_error;
    }
}
 ?>