我在使用传入的变量查询这个简单的表时遇到了麻烦:
以下是相关代码:
// MySQL connection saved to variable $db
// variable $item is passed in as the string "Camera1"
$accessQuery = "SELECT Available FROM inventory WHERE Item = '" . $item . "'";
// This outputs properly as "SELECT Available FROM inventory WHERE Item = 'Camera1'"
echo $accessQuery;
if($oldVal = mysqli_query($db, $accessQuery)){
echo $oldVal // Should be 5 - but there is no output
// echo 'Made it inside if statement' --- This line outputs correctly
}
else{
echo 'Error accessing MySQL query';
}
答案 0 :(得分:2)
您需要调用mysqli_fetch_XXX
函数以从查询中获取结果数据。
if($result = mysqli_query($db, $accessQuery)){
$row = mysqli_fetch_assoc($result);
$oldVal = $row['Available'];
echo $oldVal // Should be 5 - but $oldVal causes an error when I try to output
}
else{
echo 'Error accessing MySQL query: ' . mysqli_error($db);
}
答案 1 :(得分:0)
$accessQuery = "SELECT Available FROM inventory WHERE Item = '" . $item . "'";
if($res = mysqli_query($db, $accessQuery)){
$row = mysqli_fetch_array($res);
$oldVal = $row['Available'];
echo $oldVal;
}
else{
echo 'Error accessing MySQL query';
}