PHP代码问题:从SQL数据库获取数据时fee列无法显示
Fix for Missing
fee Column in PHP MySQL Output Hey there! I spotted the issue right away—it's a simple mismatch between your printf placeholder count and the number of variables you're passing.
Let's break it down:
- In your code, you have:
Here, you're only providing twoprintf ("%s (%s)\n", $row["id"], $row["degree"], $row["fee"]);%splaceholders but passing three variables (id,degree,fee). PHP doesn't throw an error here, but it ignores the third variable entirely—hence whyfeenever shows up.
Corrected Code
Just add a third placeholder to your printf statement, and format it however you'd like. For example:
<?php $mysqli = new mysqli("localhost", "root", "", "stu_data"); /* check connection */ if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); exit(); } $query = "SELECT id, degree, fee FROM degree"; if ($result = $mysqli->query($query)) { /* fetch associative array */ while ($row = $result->fetch_assoc()) { // Added a third %s placeholder for fee, with clear separators printf ("ID: %s | Degree: %s | Fee: %s\n", $row["id"], $row["degree"], $row["fee"]); } /* free result set */ $result->free(); } /* close connection */ $mysqli->close(); ?>
Quick Troubleshooting Tip
If you ever run into similar issues again, toss a var_dump($row); inside the loop to confirm that the fee column is actually being returned from the database. This helps rule out cases where the column might be missing from your table or misspelled in the query.
内容的提问来源于stack exchange,提问作者Sid
相关产品推荐
相关产品推荐

