Fetch values from one column of a table with PDO

Ste Yeu

I'm trying to fetch values from one table column of a MySQL db with PHP (PDO), but I have two errors in the same line, this is the line (line 7):

$query = $dbh->prepare("SELECT * FROM table_name");

And these are error messages:

Notice: Undefined variable: dbh in ... on line 7

Fatal error: Call to a member function prepare() on a non-object in ... on line 7

My complete code:

1) file config.php

    <?php
$host = "localhost";

$db_user = "user";

$db_psw = "pass";

$db_name = "database";
?>

2) file connection.php

<?php

       include("config.php");

        //collegamento
    $col = "mysql:host=$host;dbname=$db_name";

        try {
                  //tentativo di connessione
          $db = new PDO($col , "$db_user", "$db_psw");
        }
                    //gestione errori
            catch(PDOException $e) {

              echo 'Attenzione errore: '.$e->getMessage();
            }      

?>

3) file display-values.php (error in line 7)

<?php
// richiamo lo script responsabile della connessione a MySQL
require 'connection.php';
?>

<?php
$query = $dbh->prepare("SELECT * FROM table_name");
$query->execute();
$result = $query->fetchall();
?>

<?php
    echo 
    "<table border='2'>
    <tr>
    <th>ID</th>
    <th>A Number</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Why</th>
    <th>Comments</th>
    <th>Signintime</th>
    </tr>"
    ;

    foreach($result as $row)
    {
  echo "<tr>";
  echo "<td>" . $row['id'] . "</td>";
  echo "<td><a href=Student.php?studentA_num=" . $row['anum'] . ">" .$row['anum'] . " </a></td>";
  echo "<td>" . $row['first'] . "</td>";
  echo "<td>" . $row['last'] . "</td>";
  echo "<td>" . $row['why'] . "</td>";  
  echo "<td>" . $row['comments'] . "</td>";
  echo "<td>" . $row['signintime'] . "</td>";
  echo "<td> <input type=\"button\" value=\"Start Session\"onClick=\accept.php?id=" . $row['id'] . "&start=true></td>";
}

  echo "</tr>";
  echo "</table>";
?>

Note: In this code I've tried to fetch all values (just for a test), but I need to fetch data from one column "name_column" only, so how can I fix the two errors and edit the code to extract values from a specific column?

Thanks in advance!

mTorres

Your PDO object is db not dbh

The error states that:

Notice: Undefined variable: dbh in ... on line 7

PHP couldn't find an initialized variable called dbh, so dbh is undefined variable, therefore you have your notice here.

Fatal error: Call to a member function prepare() on a non-object in ... on line 7

As your code calls a method on a non existent variable, PHP is yelling this second error.

To sum up, you just need to change

$query = $dbh->prepare("SELECT * FROM table_name");

for

$query = $db->prepare("SELECT * FROM table_name");

Editing query for fetching values from 2 columns:

If you want to fetch values for 2 column only, just change your SQL query:

$query = $db->prepare("SELECT column_name_1, column_name_2 FROM table_name");

With this query you're selecting values from column_name_1 and column_name_2

I recommend you to take some basic SQL tutorial

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PDO fetch one column from table into 1-dimensional array

In Postgresql, how can I fetch an unknown # of records from a table that have M distinct values in one column?

Compare values from one column in table A and another column in table B

How I fetch values from one column based on distinct values from other column?

Pythonic way of replace values in one column from a two column table

mysql insert unique values from one column to a column of another table

TSQL - Select column values from one table to another one

How to update a column in one table based on values from a second table?

Inputing column values in one table from another related table

Inserting a values of one column from a table into another table

add column to one table based on values and conditions from another table

MSSQL Select column values from one table to another table

Updating column on one table with values from another table

Update column value of one table based on values from another table

How to fetch values from one table and insert the same values in other table

Fetch Col values from one Table for which cols are represented as Row values in Different Table

sum column values from one table in relation with values of column from another table

Join multiple values from one column, selected from another table

How to fetch respective columns from BigQuery table with values matching from another table column joined?

Find common values (Intersection) from one column of a table depending on the given values of second column of same table

how to fetch all the records from one table as XML datatype to another column of second table

Copy values from one column to another in the same table

copy certain column values from one table to anther

How to automatically load values from one table column to another in MYSQL

sql getting values from the same table and same column into one row

Insert values from one column to another in the same table

Using values from a field in one table as column name for field in another

Insert INTO from one table to another and change column values

How to create a column that has the count from one table of values in another?