SQL 查询执行成功但数据不显示

问题描述 投票:0回答:0

此代码在本地主机上有效,但在 cpanel 中无效。我无法从中访问 $row 和 columns。

print_r($row)
正确打印所有值,但是当我尝试像
$row['name']
一样使用时,它返回 null
alert($row['name'])
没有显示

代码:

<?php 
                    
include './sql/connection.php';
$sql = "SELECT id, name, category, price, description, image FROM proizvod";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
    print_r($row);
    echo "<script>alert(".$row['name'].")</script>";
    if($row['category']=='Nargila')
        $category = 'filter-narg';
    else if($row['category']=='Ukus')
        $category = 'filter-ukusi';
    else if($row['category']=='Ostalo')
        $category = 'filter-ostalo';

    echo '<div class="col-lg-4 col-md-6 shop-item '. $category .'">
        <div class="product shadow-lg rounded-2" data-aos="fade-up" data-aos-delay="100">
            <a href="shop-details.php?id='. $row['id'] .'">
                <div class="product-img">
                    <img src="./assets/img/shop/'. $row['image'] .'" class="img-fluid" alt="'. $row['description'] .'">
                    <div class="social">
                        <i class="fa-solid fa-plus fa-xs me-2"></i>Više
                    </div>
                </div>
                <div class="product-info mt-1 py-2">
                <span>'. $row['category'] .'</span>
                <h4>'. $row['name'] .'</h4>
                <h3 class="mt-3">'. $row['price']-1 .' RSD</h3>
                </div>
            </a>
            <a href="./pages/delete.php?id='. $row['id'] .'" onclick="return confirm(\'Da li si siguran da hoćeš da izbriseš?\')" 
                class="btn btn-primary-outline text-danger mb-2 admin">Izbriši</a>
            <form action="" method="POST">
                <input name="id" type="text" class="d-none" value="'. $row['id'] .'">
                <button class="btn w-100 py-3 bg-theme" name="add-to-cart" type="submit">
                    <i class="fa-solid fa-cart-plus fa-xl me-3"></i>Dodaj u korpu
                </button>
            </form>
        </div>
    </div>';
}
?>

如何访问这些值?

php mysql sql mysqli cpanel
© www.soinside.com 2019 - 2024. All rights reserved.