Save results from pdo query to a javascript variable

后端 未结 1 1817
刺人心
刺人心 2021-01-21 09:15

I am new to javascript so please be patient with me. I have a function in php which goes like this:

 public function getSubjects() {
            $stmt = $this-&g         


        
1条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 10:06

    I like to use json_encode to convert the array to json so it can be used as an array of objects in JS.

    PHP:

    public function getSubjects() {
        $stmt = $this->_db->prepare('SELECT id, subject from subjects');
        $stmt->execute();                     
        return json_encode($stmt->fetchall());
    }
    

    In javascript:

    var subs = ;
    console.log(subs);
    

    0 讨论(0)
提交回复
热议问题