简体   繁体   English

使用PDO显示数据库中的数据

[英]Displaying data from a database using PDO

I've been staring at code for hours trying to see what I'm missing. 几个小时以来,我一直在盯着代码,试图查看我所缺少的内容。 Maybe someone can point me in the right direction. 也许有人可以指出我正确的方向。

First I have a database with multiple tables. 首先,我有一个包含多个表的数据库。 One for images that stores the location and details of products. 一种用于存储产品位置和详细信息的图像。 I've built pages that let me view, edit and add products. 我建立的页面可以让我查看,编辑和添加产品。 This all works great. 这一切都很好。

I'm now trying to do something simple and its not working. 我现在正在尝试做一些简单的事情,但它不起作用。 I created a table "Contacts" in the database and added the rows "name, street, city, zip, phone, email". 我在数据库中创建了一个表“ Contacts”,并添加了“名称,街道,城市,邮政编码,电话,电子邮件”行。 I have populated this table with test data. 我已经用测试数据填充了该表。

I'm trying to get my page to read the data and display it on the screen. 我正在尝试让我的页面读取数据并将其显示在屏幕上。 I had wrote a complete page of html and screwed around with it trying to get it to work. 我写了一个完整的html页面,并试图使其正常工作。 after getting frustrated I resorted to a very simple php script below. 感到沮丧后,我诉诸于以下非常简单的php脚本。 I keep getting SQL error "Notice: Undefined index: name in C:\\wamp\\www\\woody\\Admin\\index.php on line 38" I know the first index is called "name" I have also attempted to replace "$results['name']" with "$results['0']" and "$results['1']". 我不断收到SQL错误“第38行,注意:未定义的索引:C:\\ wamp \\ www \\ woody \\ Admin \\ index.php中的名称”,我知道第一个索引称为“名称”,我也试图替换“ $ results” ['name']“和” $ results ['0']“和” $ results ['1']“。 Nothing seems to work. 似乎没有任何作用。 Anyone see what my tired eyes are missing? 有人看到我疲倦的眼睛不见了吗?

<body>

<?php
    require_once("../includes/db.php");

$sql = "SELECT * FROM contact";
$query = $conn->prepare( $sql );
$query->execute();
$results = $query->fetchAll( PDO::FETCH_ASSOC );        


echo htmlentities($results['name']);


?>








<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>  

echo htmlentities($results[0]['name']);

would be correct way because you are using fetchAll() that returns a nested array. 正确的方法,因为您正在使用返回嵌套数组的fetchAll()

or, more proper way as you are selecting many rows 或者,选择许多行时采用更正确的方式

foreach ($results as $row)
{
    echo htmlentities($row['name']);
}

If you want to select only one row, then you have to use fetch() method instead of fetchAll() . 如果只想选择一行,则必须使用fetch()方法而不是fetchAll() You may read about various fetch modes in the guide I wrote, The only proper guide on PDO 您可能会在我撰写的指南中了解到各种取指模式, 这是PDO上唯一合适的指南

If empty array is returned, than you did not populated the table with sample data 如果返回空数组,则表示您没有用示例数据填充表

There is also a possibility for the error. 也有可能发生错误。 You have to report them as described in My PDO Statement doesn't work 您必须按照“ 我的PDO声明”中的说明报告这些问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM