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

PHP与MySQL问题排查:变量显示异常及SQL语句疑问

Hey there! Let's break down the issue with your PHP code step by step and fix it up.

Problem Analysis & Fixes

First off, the biggest issue is in your odb_get_pkey function—it doesn't return any value! You're echoing the $pkey inside the function, but you never send that value back to where the function is called. That means when you run odb_get_pkey($user), it's actually returning null, turning your main query into a broken SQL statement like:

$query="select password from ouser where pkey=";

No wonder your variables aren't showing up correctly—this invalid query is throwing off all your subsequent logic.

1. Fix the odb_get_pkey Function

Add a return statement to pass the retrieved primary key back to the caller:

function odb_get_pkey($username) {
    $db=odb_connect();
    echo "Get the primary key".oLF();
    $query="select pkey from ouser where username='$username'";
    $pkey=odb_get_field($query,$db);
    echo $pkey.oLF();
    // This line was missing!
    return $pkey;
}

2. Fix the Main Query's Syntax & Safety

Assuming pkey is a string-type value (even if it's numeric, quoting is safer here), wrap the returned value in single quotes to avoid SQL syntax errors:

$query="select password from ouser where pkey='".odb_get_pkey($user)."'";

Also, a critical note: direct variable concatenation in SQL is a huge SQL injection risk. If $username or $user ever contains special characters (like a single quote), your query will break, and attackers could exploit this to access or modify your database. Whenever possible, use parameterized queries instead of raw string拼接.

Extra Troubleshooting Checks

If you still have issues displaying variables after these fixes:

  • Replace the echo $pkey in odb_get_pkey with var_dump($pkey) to see exactly what value odb_get_field is returning
  • Use var_dump($user) before calling odb_get_pkey to confirm the variable has the correct value
  • Verify that odb_connect is successfully establishing a database connection

内容的提问来源于stack exchange,提问作者PurpleCake911

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:31:07