PHP与MySQL问题排查:变量显示异常及SQL语句疑问
Hey there! Let's break down the issue with your PHP code step by step and fix it up.
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 $pkeyinodb_get_pkeywithvar_dump($pkey)to see exactly what valueodb_get_fieldis returning - Use
var_dump($user)before callingodb_get_pkeyto confirm the variable has the correct value - Verify that
odb_connectis successfully establishing a database connection
内容的提问来源于stack exchange,提问作者PurpleCake911

