mysqli 查询不返回 JSON 和谷歌图表的结果
Posted
技术标签:
【中文标题】mysqli 查询不返回 JSON 和谷歌图表的结果【英文标题】:mysqli query not returning results with JSON and Google Charts 【发布时间】:2016-10-09 06:46:38 【问题描述】:我在本地开发,一切正常。当我上传到主机时,一些数据库信息无法显示。
这是进行查询的RodgersForm.php:
<?php
require_once '../login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
var_dump($conn);
$req = mysqli_query($conn,"SELECT * FROM arodgers");
print_r($req);
if(!$req) die('Database access failed: ' . $conn->error);
$CMP = array();
foreach($req as $result)
$CMP[] = array( (int)$result['SEASON'],
(int)$result['GP'],
(int)$result['CMP'],
(int)$result['ATT'],
(double)$result['CMP%'],
(double)$result['AVG'],
(int)$result['YDS'],
(int)$result['TD'],
(int)$result['FUM'],
(int)$result['INT'],
(int)$result['LNG'],
(double)$result['QBR'],
(double)$result['RAT'],);
print_r($CMP);
$CMP = json_encode($CMP);
?>
这是请求 json 的地方:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../css/main.css">
<link rel="stylesheet" href="../css/prettyPhoto.css">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<?php include 'aRodgersForm.php';?>
<script type="text/javascript">
google.load('visualization', '1', packages: ['controls', 'table']);
google.setOnLoadCallback(init);
function init ()
drawChart();
drawTable();
function drawTable()
var data = new google.visualization.DataTable();
data.addColumn('number', 'Season');
data.addColumn('number', 'Games Played');
data.addColumn('number', 'Completions');
data.addColumn('number', 'Attempts');
data.addColumn('number', 'CMP %');
data.addColumn('number', 'AVG YDS');
data.addColumn('number', 'Yards');
data.addColumn('number', 'Touchdowns');
data.addColumn('number', 'Fumbles');
data.addColumn('number', 'Interceptions');
data.addColumn('number', 'Longest Pass');
data.addColumn('number', 'Quarterback Rating');
data.addColumn('number', 'Rating');
data.addRows(JSON.parse(<?php echo json_encode($CMP); ?>));
var table = new google.visualization.Table(document.getElementById('table_div'));
var forYDS = new google.visualization.NumberFormat(
pattern:'####');
forYDS.format(data, 0);
table.draw(data, width: '100%', height: '100%');
function drawChart ()
var data = new google.visualization.DataTable();
data.addColumn('number', 'Season');
data.addColumn('number', 'Games Played');
data.addColumn('number', 'Completions');
data.addColumn('number', 'Attempts');
data.addColumn('number', 'CMP %');
data.addColumn('number', 'AVG YDS');
data.addColumn('number', 'Yards');
data.addColumn('number', 'Touchdowns');
data.addColumn('number', 'Fumbles');
data.addColumn('number', 'Interceptions');
data.addColumn('number', 'Longest Pass');
data.addColumn('number', 'QB Rating');
data.addColumn('number', 'Rating');
data.addRows(JSON.parse(<?php echo json_encode($CMP); ?>));
var columnsTable = new google.visualization.DataTable();
columnsTable.addColumn('number', 'colIndex');
columnsTable.addColumn('string', 'colLabel');
var initState= selectedValues: [];
// put the columns into this data table (skip column 0)
for (var i = 1; i < data.getNumberOfColumns(); i++)
columnsTable.addRow([i, data.getColumnLabel(i)]);
// you can comment out this next line if you want to have a default selection other than the whole list
// you can set individual columns to be the default columns (instead of populating via the loop above) like this:
initState.selectedValues.push(data.getColumnLabel(1));
var chart = new google.visualization.ChartWrapper(
chartType: 'LineChart',
containerId: 'chart_div',
dataTable: data,
options:
title: '',
width: 1100,
height: 500,
fontSize: 19,
bold:true,
hAxis: format: '####' ,
vAxis: minValue:0
);
var columnFilter = new google.visualization.ControlWrapper(
controlType: 'CategoryFilter',
containerId: 'colFilter_div',
dataTable: columnsTable,
options:
filterColumnLabel: 'colLabel',
ui:
label: '',
allowTyping: false,
allowMultiple: true,
allowNone: false,
selectedValuesLayout: 'belowStacked'
,
state: initState
);
function setChartView ()
var state = columnFilter.getState();
var row;
var view =
columns: [0]
;
for (var i = 0; i < state.selectedValues.length; i++)
row = columnsTable.getFilteredRows([column: 1, value: state.selectedValues[i]])[0];
view.columns.push(columnsTable.getValue(row, 0));
// sort the indices into their original order
view.columns.sort(function (a, b)
return (a - b);
);
chart.setView(view);
chart.draw();
google.visualization.events.addListener(columnFilter, 'statechange', setChartView);
setChartView();
columnFilter.draw();
</script>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<ul class="pull-left">
<li><a href="../index.php">Home</a></li>
<li><a href="../enigma.php">News</a></li>
<li><a href="../slideshow.php">News</a></li>
<li><a href="../stats.php">Stats</a></li>
<li><a href="../Schedule.php">Schedule</a></li>
</ul>
<ul class="pull-right">
<li><a href="#">Log In</a></li>
<li><a href="#">Help</a></li>
</ul>
</nav>
<div class="jumbotron" id="statsJumbo">
<div id="statsHeader1">
<div class="col-xs-12 col-md-offset-1" id="statsHeader">
<div class="col-md-6 col-xs-12" id="generalStats">
<h1>Aaron Rodgers</h1></br>
<h2>QB - Green Bay Packers | 6'2" - 225lbs</h2></br></br>
<h4>Drafted 2005 1st Round, 24th Overall by Green Bay</h4></br>
<h4> College California 2003-2004</h4></br>
<h4>Expirience 12 Seasons</h4>
</div>
<div class="col-md-2 col-xs-4" id="playerPic">
<img src="http://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/8439.png&w=350&h=254">
</div>
<div class="col-md-2 col-xs-4" id="playerNumber">
<p class="statsPlayerNumber">#12</p>
</div>
</div>
</div>
<div class="col-md-12" id="table_div"></div>
<div class="col-md-4 col-md-offset-1" id="chart_div"></div>
<div class="col-md-2 col-md-offset-4" id="colFilter_div"></div>
</div>
<div class="footer col-md-12">
<div class="container text-center">
<p>Copyright (c) 2015 Wyatt Thompson</p>
</div>
</div>
<script type="text/javascript" src="../js/jquery-1.9.1.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/jquery.js"></script>
</body>
</html>
这是来自另一个页面的查询,它确实显示了回显的结果,但是 var_dump($conn) 返回 object(mysqli)#1 (0) :
$query = "SELECT * FROM arodgers WHERE season =(SELECT max(season) FROM arodgers)";
$result = $conn->query($query);
if(!$result) die('Database access failed: ' . $conn->error);
$rows = $result->fetch_array(MYSQLI_BOTH);
echo <<<_END
<p>Season: $rows[SEASON]<br />
Completions: $rows[CMP] <br />
Attempts: $rows[ATT] <br />
Yards: $rows[YDS] <br />
Completion%: $rows[CMP] <br />
Touchdowns: $rows[TD] </p>
_END;
Live Page 因此,如果您在主页上查看球员的统计数据,结果都会显示出来,但如果您单击完整的统计数据页面,它是空白的。唯一没有显示的结果是我使用 JSON for Google Charts 获得的结果。
【问题讨论】:
【参考方案1】:这似乎是免费主机使用的旧版本 PHP 的问题。我用更新的 php 搬到了另一个主机,网站运行良好。我是5.6开发的,宿主是5.2的。
【讨论】:
以上是关于mysqli 查询不返回 JSON 和谷歌图表的结果的主要内容,如果未能解决你的问题,请参考以下文章