简体   繁体   English

使用PHP和MySQL生成JSON格式的表

[英]Generating a JSON formatted table using PHP and MySQL

I am trying to generate a JSON formatted table using data from my database. 我正在尝试使用数据库中的数据生成JSON格式的表。 When doing so, I get the error PHP Fatal error: Call to a member function fetch() on a non-object in G:\\PleskVhosts\\mywebsite.com\\httpdocs\\getData.php on line 23 这样做时,我得到错误PHP Fatal error: Call to a member function fetch() on a non-object in G:\\PleskVhosts\\mywebsite.com\\httpdocs\\getData.php on line 23

I'm not sure why it's giving this error as I did something very similar on another page and it works pefectly fine. 我不确定为什么会出现此错误,因为我在另一页上做了非常相似的事情,并且效果很好。 Here is my code. 这是我的代码。

<?

$db = new PDO("mysql:host=".$servername.";dbname=".$dbname.";", $username, $password);

$query = $db->query("SELECT 'MAJOR' as 'Major' SUM(IF(MAJOR = 'Computer Science',1,0)) as CS, SUM(IF(MAJOR = 'Computer Information Systems',1,0)) as CIS, SUM(IF(MAJOR = 'Other',1,0)) as Other FROM ages");



$table = array();
$table['cols'] = array(
    array('label' => 'Major', 'type' => 'string'),
    array('label' => 'CS', 'type' => 'number'),
    array('label' => 'CIS', 'type' => 'number'),
    array('label' => 'Other', 'type' => 'number')
);

$rows = array();
while($r = $query->fetch()){
    $temp = array();
    $temp[] = array('v' => $r['Major']);
    $temp[] = array('v' => (int) $r['CS']);
    $temp[] = array('v' => (int) $r['CIS']);
    $temp[] = array('v' => (int) $r['Other']);

    $rows[] = array('c' => $temp);
}

$table['rows'] = $rows;

$jsonTable = json_encode($table);

echo $jsonTable;


?>

If you think it is not a back tick issue, as well as an error reporting issue, then run the below. 如果您认为这不是倒钩问题,也不是错误报告问题,请运行以下命令。 And change the select back ticks to single quotes. 并将选择反勾号更改为单引号。 And see what you get. 看看你得到什么。

Use the below for a template for how to setup error reporting and a try/catch block. 使用以下模板作为模板,以设置错误报告和try / catch块。

Schema 架构

create table basic_info2
(   id int auto_increment primary key,
    n_id int not null,
    name varchar(20) not null,
    date_released date not null,
    genres varchar(20) not null
);

insert basic_info2(n_id,name,date_released,genres) values (1,'a','2015-04-11','b');

PHP PHP

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$servername = "host";
$username = "dbuser";
$password = "pwd";
$database = "dbname";

try {
    //Make your connection handler to your database
    $conn = new PDO("mysql:host=".$servername.";dbname=".$database, $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));

    $sql = "SELECT `N_ID`, `NAME`, `DATE_RELEASED`, `GENRES` FROM basic_info2 ORDER BY N_ID DESC LIMIT 10";
    $stmt = $conn->prepare($sql);
    //Execute the query
    $stmt->execute();
    $result = $stmt->fetchAll();
    //Fetch the results
    foreach ($result as $row) {
        echo '<p>'.$row['NAME'].'</p>';
    }

} catch(PDOException $e) {
    echo $e->getMessage();
    die();
}
?>

Edit 编辑

concerning you comment below, this is what you had 关于你下面的评论,这就是你所拥有的

SELECT MAJOR as 'MAJOR'
SUM(IF(MAJOR = 'Computer Science',1,0)) as CS, 
SUM(IF(MAJOR = 'Computer Information Systems',1,0)) as CIS, 
SUM(IF(MAJOR = 'Other',1,0)) as Other 
FROM ages

this is what you need (just a missing comma line 1) 这就是您需要的(只是缺少逗号的第1行)

SELECT MAJOR as 'MAJOR',
SUM(IF(MAJOR = 'Computer Science',1,0)) as CS, 
SUM(IF(MAJOR = 'Computer Information Systems',1,0)) as CIS, 
SUM(IF(MAJOR = 'Other',1,0)) as Other 
FROM ages

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM