php - Displaying data from a database using PDO -
i've been staring @ code hours trying see i'm missing. maybe can point me in right direction.
first have database multiple tables. 1 images stores location , details of products. i've built pages let me view, edit , add products. works great.
i'm trying simple , not working. created table "contacts" in database , added rows "name, street, city, zip, phone, email". have populated table test data.
i'm trying page read data , display on screen. had wrote complete page of html , screwed around trying work. after getting frustrated resorted simple php script below. keep getting sql error "notice: undefined index: name in c:\wamp\www\woody\admin\index.php on line 38" know first index called "name" have attempted replace "$results['name']" "$results['0']" , "$results['1']". nothing seems work. see tired eyes missing?
<body> <?php require_once("../includes/db.php"); $sql = "select * contact"; $query = $conn->prepare( $sql ); $query->execute(); $results = $query->fetchall( pdo::fetch_assoc ); echo htmlentities($results['name']); ?> <!-- jquery (necessary bootstrap's javascript plugins) --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <!-- include compiled plugins (below), or include individual files needed --> <script src="js/bootstrap.min.js"></script>
echo htmlentities($results[0]['name']);
would correct way because using fetchall()
returns nested array.
or, more proper way selecting many rows
foreach ($results $row) { echo htmlentities($row['name']); }
if want select 1 row, have use fetch()
method instead of fetchall()
. may read various fetch modes in guide wrote, the proper guide on pdo
if empty array returned, did not populated table sample data
there possibility error. have report them described in my pdo statement doesn't work
Comments
Post a Comment