使用PDO显示数据库中的数据 [英] Displaying data from a database using PDO

查看:111
本文介绍了使用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.

我现在正在尝试做一些简单的事情,但它不起作用.我在数据库中创建了一个表"Contacts",并添加了名称,街道,城市,邮政编码,电话,电子邮件"行.我已经在该表中填充了测试数据.

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". I have populated this table with test data.

我正试图让我的页面读取数据并将其显示在屏幕上.我写了一个完整的html页面,并试图使其正常工作.感到沮丧之后,我诉诸于下面的一个非常简单的php脚本.我不断收到SQL错误第38行,C:\ wamp \ www \ woody \ Admin \ index.php中的通知:未定义的索引:名称",我知道第一个索引称为名称",我也试图替换"$ results" ['name']和" $ results ['0']和" $ results ['1'].似乎没有任何作用.有人看到我疲倦的眼睛不见了吗?

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. after getting frustrated I resorted to a very simple php script below. 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']". 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']);

将是正确的方法,因为您正在使用返回嵌套数组的fetchAll().

would be correct way because you are using fetchAll() that returns a nested array.

或者是选择许多行时使用的更正确的方法

or, more proper way as you are selecting many rows

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

如果只想选择一行,则必须使用fetch()方法而不是fetchAll().您可能会在我写的指南中了解各种获取模式,有关PDO的唯一正确指南

If you want to select only one row, then you have to use fetch() method instead of fetchAll(). You may read about various fetch modes in the guide I wrote, The only proper guide on PDO

如果返回空数组,那么您没有用示例数据填充表

If empty array is returned, than you did not populated the table with sample data

也有可能发生错误.您必须按照我的PDO声明无效

There is also a possibility for the error. You have to report them as described in My PDO Statement doesn't work

这篇关于使用PDO显示数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆