2012-12-10 145 views
0
<?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

+0

,当你搜索过这些错误消息你发现了什么?你*确实*搜寻错误,对吧? – Charles

+0

错误信息的含义非常明确。为了使用mysqli_num_rows(),传入它的第一个参数必须是mysqli_result。那是什么?数据库查询的结果。我在搜索答案时得到的结果是 – TJz

+0

啊,但是当你执行一个查询时,事件* other *不是[语句句柄](http://php.net/class.mysqli-stmt)和[results](http ://php.net/class.mysqli-result)可以被返回。 [query'](http://php.net/mysqli.query)在查询工作但没有结果集时返回布尔值'true',当出现错误时返回'false'。你将需要在你的代码中处理这个。 – Charles

回答

2

准确地说:您没有明确设置时区,也没有在php.ini文件中设置一个时区。

添加到您的代码的开始:

date_default_timezone_set("America/New_York"); 
+0

好的,修复了时区错误 – TJz

+0

至于连接问题,请检查以确保您使用正确的用户名和密码连接到正确的服务器。 –

+0

我再次检查,信息是正确的。它会与权限有关吗?我在下面的db_vars.php文件中使用了相同的信息,并且一切正常。 – TJz