<?php
date_default_timezone_set("America/New_York");
require("db_vars.php");
// Connect to the database
$dbc = mysqli_connect($db_hostname, $db_database, $db_username, $db_password);
// Custom function to draw a bar graph given a data set, maximum value, and image filename
function draw_bar_graph($width, $height, $data, $max_value, $filename) {
// Create the empty graph image
$img = imagecreatetruecolor($width, $height);
// Set a white background with black text and gray graphics
$bg_color = imagecolorallocate($img, 255, 255, 255); // white
$text_color = imagecolorallocate($img, 255, 255, 255); // white
$bar_color = imagecolorallocate($img, 0, 0, 0); // black
$border_color = imagecolorallocate($img, 192, 192, 192); // light gray
// Fill the background
imagefilledrectangle($img, 0, 0, $width, $height, $bg_color);
// Draw the bars
$bar_width = $width / ((count($data) * 2) + 1);
for ($i = 0; $i < count($data); $i++) {
imagefilledrectangle($img, ($i * $bar_width * 2) + $bar_width, $height,
($i * $bar_width * 2) + ($bar_width * 2), $height - (($height / $max_value) * $data[$i][1]), $bar_color);
imagestringup($img, 5, ($i * $bar_width * 2) + ($bar_width), $height - 5, $data[$i][0], $text_color);
}
// Draw a rectangle around the whole thing
imagerectangle($img, 0, 0, $width - 1, $height - 1, $border_color);
// Draw the range up the left side of the graph
for ($i = 1; $i <= $max_value; $i++) {
imagestring($img, 5, 0, $height - ($i * ($height / $max_value)), $i, $bar_color);
}
// Write the graph image to a file
imagepng($img, $filename, 5);
imagedestroy($img);
} // End of draw_bar_graph() function
// if (mysqli_num_rows($data) != 0) {
// First grab the user's responses from the response table (JOIN to get the topic and category names)
$query = "SELECT ts.species_name, COUNT(ti.species_id) " .
"FROM tree_species ts " .
"INNER JOIN tree_individuals ti ON ts.species_id = ti.species_id " .
"GROUP BY ts.species_name'";
$data = mysqli_query($dbc, $query);
$tree_totals = array();
while ($row = mysqli_fetch_array($data)) {
array_push($tree_totals, $row);
}
// Generate and display the mismatched category bar graph image
echo '<h4>Mismatched category breakdown:</h4>';
draw_bar_graph(480, 240, $tree_totals, 5, 'treetotalgraph.png');
echo '<img src="treetotalgraph.png" alt="Tree total graph" /><br />';
//}
mysqli_close($dbc);
?>
生成以下错误
警告:mysqli_query()要求参数1为mysqli,第54行的G:\ Students \ test.php中给出布尔值
警告:mysqli_fetch_array()要求参数1为mysqli_result,在第56行的G:\ Students \ test.php中给出null
类别细分不匹配:
警告:imagepng()[function.imagepng]:无法打开'treetotalgraph.png'进行写入:第43行的G:\ Students \ test.php中的权限被拒绝
警告:mysqli_close()要求参数1为mysqli,第68行的G:\ Students \ test.php中给出布尔值
答案 0 :(得分:2)
究竟是什么意思:你没有明确设置时区,也没有在php.ini文件中设置一个时区。
将其添加到代码的开头:
date_default_timezone_set("America/New_York");