PHP从MySQL读取坐标绘制Canvas矩形异常:仅显示单个矩形
问题描述
我尝试在1000×1000的画布上绘制200个不同颜色的矩形,每个矩形的坐标取自MySQL表的info_x和info_y字段,根据表中其他字段的情况设置颜色。单独提取数据打印一切正常,但绘制时只显示一个矩形,求解决方法。
以下是我的代码:
<?php //Create a 1000 x 1000 image $canvas = imagecreatetruecolor(1000, 1000); // Allocate colors $pink = imagecolorallocate($canvas, 255, 0, 255); $white = imagecolorallocate($canvas, 255, 255, 255); $green = imagecolorallocate($canvas, 132, 135, 28); //-------------------------------------- $con=mysqli_connect("ip","name","pass","dabase"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $c=0; $query = mysqli_query($con,"SELECT * FROM tbl_info1"); //<!--- PRIMA tabella --- NUMERI L> $results = $query or die (mysqli_error()); if(mysqli_num_rows($results) != 0){ while($row= mysqli_fetch_array($results)) $c++; $posto=$row['info_posto']; $dispo=$row['info_disponibile']; $ris=$row['info_cognome']; $xs=50;// offeset secondo angolo x $ys=50;// offeset secondo angolo y $x= $row['info_x'] ;// coordinata x da sql $y= $row['info_y'] ;//coordinata y da sql if($dispo==1 and $ris!=RISERVATO){ imagerectangle($canvas, $x, $y, $x+$xs, $y+$ys $pink); // posto occupato //echo "occupato "; } if($dispo==0 and $ris!=RISERVATO){ imagerectangle($canvas, $x, $y, $x+$xs, $y+$ys, $white); // posto libero //echo "libero "; } if($dispo==1 and $ris==RISERVATO){ imagerectangle($canvas, $x, $y, $x+$xs, $y+$ys, $green); // posto riservato //echo "riservato "; } //echo $c ;// solo un contatore , mi server per saper se il clico venifa effettuato } // Output and free from memory header('Content-Type: image/jpeg'); imagejpeg($canvas); imagedestroy($canvas); ?>
问题排查与修复
你的代码存在3个关键问题,直接导致只绘制了最后一条数据的矩形:
- while循环缺少大括号:
while($row= mysqli_fetch_array($results))后未用{}包裹循环体,仅$c++;在循环内执行,其余代码仅在循环结束后执行一次,自然只会处理最后一条数据。 - imagerectangle参数语法错误:第一个if分支里的函数调用缺少逗号分隔最后两个参数(
$y+$ys $pink应该是$y+$ys, $pink),会触发语法错误终止后续执行。 - 未定义常量处理错误:
RISERVATO如果是字符串值,必须用引号包裹('RISERVATO'),否则PHP会将其视为未定义常量,触发错误。
修复后的完整代码:
<?php // 创建1000×1000画布 $canvas = imagecreatetruecolor(1000, 1000); // 分配颜色 $pink = imagecolorallocate($canvas, 255, 0, 255); $white = imagecolorallocate($canvas, 255, 255, 255); $green = imagecolorallocate($canvas, 132, 135, 28); // 填充背景色(避免默认黑色背景导致部分矩形不可见) imagefill($canvas, 0, 0, imagecolorallocate($canvas, 200, 200, 200)); // 连接数据库 $con = mysqli_connect("ip", "name", "pass", "dabase"); // 检查连接,失败则终止 if (mysqli_connect_errno()) { echo "连接MySQL失败: " . mysqli_connect_error(); exit; } $c = 0; $query = mysqli_query($con, "SELECT * FROM tbl_info1"); // 检查查询是否成功 if (!$query) die(mysqli_error($con)); if (mysqli_num_rows($query) != 0) { // 用大括号包裹完整循环体 while ($row = mysqli_fetch_array($query)) { $c++; $posto = $row['info_posto']; $dispo = $row['info_disponibile']; $ris = $row['info_cognome']; $xs = 50; // x偏移量 $ys = 50; // y偏移量 $x = $row['info_x']; $y = $row['info_y']; // 修复参数逗号,将RISERVATO改为字符串 if ($dispo == 1 && $ris != 'RISERVATO') { imagerectangle($canvas, $x, $y, $x + $xs, $y + $ys, $pink); } elseif ($dispo == 0 && $ris != 'RISERVATO') { imagerectangle($canvas, $x, $y, $x + $xs, $y + $ys, $white); } elseif ($dispo == 1 && $ris == 'RISERVATO') { imagerectangle($canvas, $x, $y, $x + $xs, $y + $ys, $green); } } } // 输出图片并释放内存 header('Content-Type: image/jpeg'); imagejpeg($canvas); imagedestroy($canvas); ?>
额外提示:
- 建议开启PHP错误提示(
error_reporting(E_ALL); ini_set('display_errors', 1);),方便快速排查语法问题。 - 用
mysqli_fetch_assoc替代mysqli_fetch_array,可避免返回冗余的索引数组,节省内存。 - 数据库连接信息不要硬编码,建议存入配置文件或环境变量。
内容的提问来源于stack exchange,提问作者iz1kbp
相关产品推荐
相关产品推荐

