Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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
在LAMP服务器上使用PHP从MySQLi输出JSON api_Php_Mysql_Json_Mysqli - Fatal编程技术网

在LAMP服务器上使用PHP从MySQLi输出JSON api

在LAMP服务器上使用PHP从MySQLi输出JSON api,php,mysql,json,mysqli,Php,Mysql,Json,Mysqli,我正在构建一个应用程序,该应用程序在RDS实例上有一个MySQL db,我希望能够通过api向web/mobile应用程序提供数据。因此,我为web服务构建了一个UbuntuLamp服务器EC2实例 在/var/www/html/I中,我创建了我的第一个php脚本 <?php class ArticlesAPI { private $db; function __construct() { $this->db = new mysqli("mysql-

我正在构建一个应用程序,该应用程序在RDS实例上有一个MySQL db,我希望能够通过api向web/mobile应用程序提供数据。因此,我为web服务构建了一个UbuntuLamp服务器EC2实例

在/var/www/html/I中,我创建了我的第一个php脚本

<?php
class ArticlesAPI {
    private $db;

    function __construct() {
        $this->db = new mysqli("mysql-host.rds.amazonaws.com", "user", "password", "dbname");
        $this->db->autocommit(FALSE);
    }

    function __destruct() {
        $this->db->close();
    }

    function top() {
        $stmt = $this->db->prepare("SELECT article_id, title, summary, article, image FROM top_articles;");
        $stmt->execute();
        $stmt->bind_result($article_id, $title, $summary, $article, $image);

        while ($stmt->fetch()) {
            echo "$article_id";
            echo "$title";
            echo "$summary";
            echo "$article";
            echo "$image";
        }
        $stmt->close();
    }
}

$api = new ArticlesAPI;
$api->top();

?>

它返回终端中查询中的最后一条记录,当我试图通过浏览器使用它时,它似乎在查询数据库,但它不返回任何内容。我不熟悉JSON、PHP和API。任何帮助都将不胜感激。

这是一种创建所需输出的简单方法

<?php
class ArticleAPI {

        function top() {
                $db = new  mysqli("mysql-host.rds.amazonaws.com", "user", "password", "dbanme");
                $results = $db->query("SELECT article_id, title, summary FROM top_articles");

                $articles = array();
                while($article = $results->fetch_assoc()){
                        $article_id = $article['article_id'];
                        $articles[$article_id][] = $article['title'];
                        $articles[$article_id][] = $article['summary'];
                }
                $results->close();
                $db->close();
                $json = json_encode($articles);
                echo $json;

        }
}
$api = new ArticleAPI;
$api->top();
?>

$this->rows
$rows
是两个完全不同的变量。同样,由于您一直以这种方式分配给
$rows['…']
,因此您会一直覆盖前几行的结果,最后只输出最后一行结果。
<?php
class ArticleAPI {

        function top() {
                $db = new  mysqli("mysql-host.rds.amazonaws.com", "user", "password", "dbanme");
                $results = $db->query("SELECT article_id, title, summary FROM top_articles");

                $articles = array();
                while($article = $results->fetch_assoc()){
                        $article_id = $article['article_id'];
                        $articles[$article_id][] = $article['title'];
                        $articles[$article_id][] = $article['summary'];
                }
                $results->close();
                $db->close();
                $json = json_encode($articles);
                echo $json;

        }
}
$api = new ArticleAPI;
$api->top();
?>