php - 从关联数组中回显特定数据?

标签 php mysql sql arrays associative-array

这是一个菜谱项目,用户使用表单并输入自己的菜谱。我已经完成了整个部分的工作。现在我正在尝试从数据库中检索食谱的信息并将其显示在页面上。无论如何,这是我现在拥有的代码。

<?php
include_once('includes/connection.php');
include_once('includes/recipe.php');
$recipe = new Recipe;

if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $basic_data = $recipe->fetch_data($id);

    $sql1 = $pdo->prepare("SELECT recipes.*, categories.* FROM recipes 
      INNER JOIN categories 
      ON (recipes.category_ID = categories.category_ID) 
      WHERE recipes.recipe_id = ?");
    $sql1->bindValue(1, $id);
    $sql1->execute();

    $results1 = $sql1->execute(); 
    echo $results1 = $sql1->fetchAll(PDO::FETCH_ASSOC);
    var_dump($results1);
?>

这是当此代码运行时我在网页上看到的内容。我以前从未使用过关联数组,所以我真的不知道如何回显其中包含的特定数据。出于这个问题的目的,我将重点放在“category_name”上,并且我试图仅回显该配方的“category_name”中存储的值,该值恰好是“none”。如何仅将“none”一词回显到页面?

    Arrayarray(1) { 
        [0]=> array(15) { 
            ["recipe_ID"]=> string(1) "1" 
            ["recipe_name"]=> string(20) "English Muffin Pizza" 
            ["category_ID"]=> string(1) "1" 
            ["servings_ID"]=> string(2) "13" 
            ["prep_hours"]=> string(1) "0" 
            ["prep_minutes"]=> string(2) "20" 
            ["cook_hours"]=> string(1) "0" 
            ["cook_minutes"]=> string(2) "10" 
            ["oven_temp"]=> string(3) "350" 
            ["directions"]=> string(402) "These are the directions." 
            ["extra_comments"]=> string(37) "This is a short extra comment." 
            ["recipe_favorite"]=> string(1) "1" 
            ["recipe_photo"]=> string(0) "" 
            ["created"]=> string(19) "2014-09-20 10:22:39" 
            ["category_name"]=> string(4) "none" } 
    }

如果我可以向您提供更多信息,请告诉我。这是漫长的一天,我可能错过了一些可以帮助你帮助我的事情。

最佳答案

如果您严格尝试在此数组中回显category_name,您只需编写:

echo $results1[0]['category_name'];

关于php - 从关联数组中回显特定数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26051765/

相关文章:

大型表中排序查询的 mysql 性能改进

php - Yii 框架 + WP 风格(使用 WP 数据库表)后层次结构 : I need to order categories and subcategories

php - Laravel - 四层深度关系

mysql - 使用 mysql 连接 3 个表

mysql - 为什么当我使用 UNION ALL nodejs, mysql 时 mysql SELECT 语句不起作用

sql - 如何选择记录数,然后按外键对它们进行分组?

php - 为 Woocommerce 中的登录用户隐藏 "Add to Cart"按钮

PHP 检查 NULL

mysql - 选择 sql 中的查询并在每一行上进行测试

sql - 在回退到默认值的树状表中选择数据的最佳方法