如何从关联数组查询字符串中获取值


how to get value form Associative Arrays query string

$resturant_category_product_query = "SELECT * FROM bb_product p WHERE p.resturant_category_id ='$resturant_category_id'";
foreach ($resturant_category_product_query as $resturant_category_product) {
        $resturant_category_product_data[] = array(
            'resturant_category_id' =>$resturant_category_product['resturant_category_id'],
        );
}

简单示例:-

<?php
$mysqli_connection = new mysqli("localhost", "my_user", "my_password", "world");
$sql = "SELECT * 
        FROM bb_product p 
        WHERE p.resturant_category_id ='".$mysqli_connection->->real_escape_string($resturant_category_id)."'";
if ($result = $mysqli_connection ->query($sql)) 
{
    while($row=$result->fetch_assoc($result))
    {
        $resturant_category_product_data[] = array('resturant_category_id'=>$row['resturant_category_id']);
    }
    $result->close();
}

使用mysqli创建一个连接数据库的对象。

然后设置要执行的SQL字符串。注意,它使用连接对象来使用类来转义字符串(以防止SQL注入)。

该字符串随后由连接对象的查询方法使用,返回一个结果对象。

然后执行WHILE循环从结果对象返回的每一行(每一行由fetch_assoc作为关联数组返回)。注意,返回的行被放入$row变量中。