<?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 中给出