mysql查询疑难解答

时间:2015-01-10 18:56:10

标签: mysql echo

我在使用传入的变量查询这个简单的表时遇到了麻烦: enter image description here

以下是相关代码:

// 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';
}

2 个答案:

答案 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';
}