我想通过简单的脚本从数据库中获取数据
<a href='category.php?CAT=Shoes'>Shoes</a>
然后简单地在“CAT”列中显示具有特定数据的所有行,如下所示:
$CAT = $_GET['CAT'];
$sql = "SELECT * FROM Shop WHERE CAT = $CAT" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo"
... results
"}}
问题是这个脚本适用于INT(例如SELECT CAT = 5,类似于category.php?CAT = 5)但不适用于VARCHAR(SELECT CAT =类似category.php?CAT = Shoes)。现在我不确定为什么会这样。 出错:试图获取非对象的属性
答案 0 :(得分:0)
$sql = "SELECT * FROM Shop WHERE CAT = '$CAT'"
答案 1 :(得分:0)
您需要将$cat
作为字符串
$cat = $_GET['CAT'];
$sql = "SELECT * FROM Shop WHERE CAT = '{$cat}'" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<pre>' . print_r($row, true) . '</pre><br/>';
}
}