You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:
    printf ("%s (%s)\n", $row["id"], $row["degree"], $row["fee"]);
    
    Here, you're only providing two %s placeholders but passing three variables (id, degree, fee). PHP doesn't throw an error here, but it ignores the third variable entirely—hence why fee never 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:48:01