PHP mySQL服务引发未捕获的错误:不在对象上下文中时使用$ this

时间:2019-05-28 13:58:42

标签: php mysql vps

我创建了一个简单的PHP服务,以查询托管在Dreamhosted VPS上的mysql数据库。我已经安装了该服务,但获得

  

未捕获的错误:不在对象上下文中时使用$ this

我浏览了所有提到此问题的页面,但终生无法解决。我不是PHP开发人员,但已按照以下教程进行操作:

'https://www.codeofaninja.com/2017/02/create-simple-rest-api-in-php.html'

但是正在连接到已经建立的数据库。测试端点是:

'https://libertycon.org/api/guest/read.php'

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);

    // required header
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");

    // include database and object files
    include_once '../config/database.php';
    include_once '../objects/guest.php';

    // instantiate database and category object
    echo "Getting Connection";
    $database = new Database();
    $db = $database->getConnection();

    // initialize object
    $guest = new Guest($db);

    // query guests
    echo "Calling Read Function";
    public $stmt = read();
    $num = $stmt->rowCount();

    // check if more than 0 record found
    if($num>0){

        // guest array
        $guest_arr=array();
        $guest_arr["records"]=array();

        // retrieve our table contents
        // fetch() is faster than fetchAll()
        // http://stackoverflow.com/questions/2770630/pdofetchall-vs-    pdofetch-in-a-loop
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            // extract row
            // this will make $row['name'] to
            // just $name only
            extract($row);

            $guest_item=array(
                "id" => $id,
                "name" => $name,
                "bio" => html_entity_decode($bio),
                "image" => $image,
                "type" => $type
            );

            array_push($guest_arr["records"], $guest_item);
        }

        // set response code - 200 OK
        http_response_code(200);

        // show guests data in json format
        echo json_encode($guest_arr);
    } else {

        // set response code - 404 Not found
        http_response_code(404);

        // tell the user no guest was found
        echo json_encode(
            array("message" => "No guests found.")
        );
    }
**THE FOLLOWING FUNCTION WAS IN THE WRONG FILE AND NOT IN A CLASS AND CAUSING THE ISSUE**   
    // read guests
    function read(){
        // select all query
        $query = "SELECT * FROM " . $this->table_name . " 
        ORDER BY
        p.name DESC";

        // prepare query statement
        echo "Preparing Query...";
        $stmt = $this->conn->prepare($query);  // ***ERROR OCCURRING HERE***
        echo "Not hitting this line of output...";

        // execute query
        $stmt->execute();

        return $stmt;        
    }
?>

我希望输出能够从数据库中获得来宾的json数组,但是相反,我看到了我所有的测试输出字符串,直到错误注释行,如果您点击了测试开始时列出的测试端点,就会抛出该错误帖子:

Connecting to DBCalling Read()functionPreparing Query...
Fatal error:  Uncaught Error: Using $this when not in object context in /home/libertycon/libertycon.org/api/guest/read.php:77
Stack trace:
0 /home/libertycon/libertycon.org/api/guest/read.php(23): read()
1 {main}
  thrown in /home/libertycon/libertycon.org/api/guest/read.php on line <77

0 个答案:

没有答案