简体   繁体   中英

mysqli query not returning results with JSON and Google Charts

I developed locally and everything worked fine. When I uploaded to the host, some database information is failing to show.

This is aRodgersForm.php where query is made:

<?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);
?>

And this is where the json is requested:

    <!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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2005 1st Round, 24th Overall by Green Bay</h4></br>
    <h4> College &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; California 2003-2004</h4></br>
    <h4>Expirience &nbsp;&nbsp;&nbsp;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>

This is a query from another page that does display results from the echo, but the var_dump($conn) returns 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 So if you look at the stats of the players on the home page the results are all shown, but if you click on the full stats page it is blank. The only results that are not showing are the ones I get with JSON for Google Charts.

It seems it was a problem with an older version of PHP the free host was using. I moved to a different host with updated php and the website works fine. I developed in 5.6 and the host had 5.2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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