0

I have asked a few hours ago, how could I create an object for the connection on the database, and I have been advised, not to bother creating an object just for the connection. I think I did.... next step, was to display the content of a simple table to check it works, and again I am stuck, not seeing errors but "Connected successfully Chef Object ( )".

My project structure:

  1. Recipe book
    • classes
      • chef.php
    • includes
      • header.php
      • footer.php
      • db_connection.php
    • index.php

This is my db_connection.php file, which I have placed in a folder called "includes":

//Database constants
defined('DB_SERVER') ? null : define('DB_SERVER', "localhost");
defined('DB_USER') ? null : define('DB_USER', "someuser");
defined('DB_PASS') ? null : define('DB_PASS', "somepass");
defined('DB_NAME') ? null : define('DB_NAME', "somedbname");
defined('DB_DRIVER') ? null : define('DB_DRIVER', "mysql");


try {
      $conn = new PDO(DB_DRIVER . ":dbname=" . DB_NAME . ";host=" . DB_SERVER, DB_USER, DB_PASS);
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      echo "Connected successfully";
} catch(PDOException $e) {
      echo "Connection failed: " . $e->getMessage();
    }

Then, I have, in chef.php:

 class Chef {

       public function display_chefs($sql) {
       include 'includes/db_connection.php';   
         try {
           return $conn->query($sql);
         }  catch(PDOException $e) {
            echo 'Error: ' . $e->getMessage() . '<br />';
            return array();
         }
       return true;
     }
   }

I don't know much about object oriented, the point of this project is to learn. So, I have taken a function that I used in a procedural way to display content from a table (see code below):

   public function display_chefs($sql) {
       include 'includes/db_connection.php';   
       try {
       return $conn->query($sql);
       }  catch(PDOException $e) {
        echo 'Error: ' . $e->getMessage() . '<br />';
        return array();
    }
    return true;
    }

And then, on index.php I am instantiating the chef object and trying to perform the query...

<?php require_once('includes/db_connection.php'); 
 require_once('classes/chef.php'); 
 include('includes/header.php'); 

$chef = new Chef;

$sql = "SELECT * FROM chefs";
$chef->display_chefs($sql);

print_r($chef);

include('includes/footer.php'); ?>

But as I said, the only thing appearing on index.php if I look at the browser is "Connected successfully Chef Object ()".

The data I want to pull is from the table called "chefs", which has a column "id", column "name" and column "country". Should this appear in the chef class somehow?

Thanks in advance :)

eve_mf
  • 795
  • 3
  • 12
  • 36
  • 1
    You `return` the query result from `display_chefs`, and then you're not doing anything with it. At the very least you'll need `$chefs = $chef->display_chefs($sql)`, and then do something appropriate to output that query result (a `PDOStatement` object). – deceze Nov 16 '16 at 16:28
  • 1
    You've got a few issues here.. a refactor is definitely needed.. But you're returning the query result object (`PDOStatement object`).. you're going to want to use that response to get your result.. – Pogrindis Nov 16 '16 at 16:29
  • I don't know where to start. Step by step please. So, I understand why I need something like $chefs = $chef-> ..... , but I don't get what you mean with "do something appropiate to output the query". In a procedural way.... I don't need to bind any parameters or either execute the query do display a SELECT query. right? Do you mean something like FetchAll? – eve_mf Nov 16 '16 at 16:38
  • 1
    You don't need FetchAll. but even after fetchAll you wiil have to **do something to display the data.** – Your Common Sense Nov 16 '16 at 16:41
  • I closed this question linking to a similar one where you can find an example. – Your Common Sense Nov 16 '16 at 16:42
  • oooh you mean a foreach loop or something like it! It is displaying the chef name now adding this $chefs = $chef->display...... and a foreach loop. But.... I am returning $chefs->fetchAll(PDO::FETCH_ASSOC). I am going to keep going, and see how it goes.... Thank you for your patience!!!! – eve_mf Nov 16 '16 at 16:46
  • Your Common Sense..... it is fine you closed the question, it is working as I wanted now, but, I have looked at the post you marked as existing question, and this makes it not to work as I expect!! Because if I follow what "PDO Return All Rows" post says, I change the result to fetch, instead of fetchAll, then, I needed to modify the output where I had foreach ($chefs as $item) { echo $item['name'] . ' from ' . $item[''country]} to echo $item, but it is only displaying the data from the first row.... so it is not "SELECTING * ALL from chefs". Anyway, it doesn't matter much anymore :) thanks – eve_mf Nov 16 '16 at 16:55

0 Answers0