<?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);
?>
生成以下錯誤PHP錯誤,布爾邏輯
警告:mysqli_query()預計參數1是mysqli的,布爾G中給出:在線路\學生\ test.php的54
警告:mysqli_fetch_array()預計參數1被mysqli_result,在G中空給出:
警告::imagepng()[F上線56
不匹配的類別細分\學生\ test.php的無法打開'treetotalgraph.png'寫入:權限被拒絕G:\ Students \ test.php在線43
警告:mysqli_close()期望參數1爲mysqli,布爾型給出G :\ Students \ test.php on line 68
,當你搜索過這些錯誤消息你發現了什麼?你*確實*搜尋錯誤,對吧? – Charles
錯誤信息的含義非常明確。爲了使用mysqli_num_rows(),傳入它的第一個參數必須是mysqli_result。那是什麼?數據庫查詢的結果。我在搜索答案時得到的結果是 – TJz
啊,但是當你執行一個查詢時,事件* other *不是[語句句柄](http://php.net/class.mysqli-stmt)和[results](http ://php.net/class.mysqli-result)可以被返回。 [query'](http://php.net/mysqli.query)在查詢工作但沒有結果集時返回布爾值'true',當出現錯誤時返回'false'。你將需要在你的代碼中處理這個。 – Charles