故障排除 mysql 查询
Troubleshooting mysql Query
我在使用传入变量查询这个简单的 table 时遇到问题:
相关代码如下:
// 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';
}
您需要调用 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);
}
$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';
}
我在使用传入变量查询这个简单的 table 时遇到问题:
相关代码如下:
// 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';
}
您需要调用 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);
}
$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';
}