PHP图片无法获取问题排查:附相关业务代码片段
Let’s break down the likely causes of your image not loading, based on the code snippet you shared:
You’re accessing an invalid variable after the loop ends
Yourwhileloop usesmysqli_fetch_object($result2)to populate the$imagesarray, but once the loop finishes,$rowbecomesfalse(sincemysqli_fetch_objectreturnsfalsewhen there are no more rows to fetch). Trying to access$row['Cover_Art']after the loop will throw an error—you should pull the cover art value from the$imagesarray instead. For example, to get the first game's cover:if (!empty($images)) { $firstGameCover = $images[0]->Cover_Art; // Using object syntax since you stored objects }Your image tag is malformed and incomplete
The line$cover = $row['Cover_Art'] .'"src="G...';is clearly truncated and has incorrect syntax for building an<img>tag. You need to construct the tag properly, making sure to wrap the source path in quotes and include the full valid path. For example:if (!empty($images)) { // Adjust the base path to match where your images are stored on the server $baseImagePath = '/game-assets/covers/'; $coverSrc = $baseImagePath . $images[0]->Cover_Art; // Use htmlspecialchars to prevent XSS vulnerabilities $coverHtml = '<img src="' . htmlspecialchars($coverSrc) . '" alt="Game Cover">'; }No error checking for your database query
You don’t verify if yourmysqli_querycall succeeded. If there’s a syntax error in$query2(like a typo in a table/column name),$result2will befalse, your loop won’t run, and$imageswill be empty. Add basic error handling to catch this:$result2 = mysqli_query($db_server, $query2); if (!$result2) { // Log this error instead of displaying it in production! die('Database query failed: ' . mysqli_error($db_server)); }Potential issues with the
Cover_Artpath
Double-check what’s stored in yourgametable’sCover_Artcolumn:- If it’s a full URL, confirm the URL is valid and the image exists at that location.
- If it’s just a filename or relative path, make sure you’re prepending the correct base path (like the directory where your images are stored on the server) to it. Also, ensure the image file exists in that directory and has proper read permissions for the server.
Confusion between object and array syntax
Since you’re usingmysqli_fetch_object, each row in$imagesis an object—so you need to access fields with->(e.g.,$row->Cover_Art) instead of array syntax ($row['Cover_Art']). If you prefer array access, switch tomysqli_fetch_assoc()instead.
内容的提问来源于stack exchange,提问作者MuzzaMurray

