-1

I have connection.php file as :

<?php

       $dsn='mysql:host=localhost;dbname=sakila';
   $hostname='localhost';
   $username='root';
   $password='';
    $PDO_ob = new PDO($dsn, $username, $password);
   try {
        $PDO_ob = new PDO($dsn, $username, $password);
        $PDO_ob->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          echo " connection success";
    } catch (PDOException $ex) {
        echo 'Connection failed: ' . $ex->getMessage();
    }
   ?>  
And functions.php  as :

    <?php 
    include "../category_prodct_pdo/connection.php";

    function get_products(){
        global $PDO_ob;


        $sql="SELECT * FROM products";
        $products=$PDO_ob->query($sql); 
        return $products;
    }
    ?>

When I delete the line glopal $PDO_op; I receive n error message " Undefined variable: PDO_ob " I know that using global variables in functions is not the best implement so how can make this work without to use global inside the function Thank You in Advance

dumro
  • 1
  • 1
  • 3
    `function get_products($PDO_ob) { ... }` spooky. http://www.phptherightway.com/#dependency_injection and https://stackoverflow.com/a/16959577/1064767 and security/variable scope aren't really related in the code you've posted, but if it makes you write better code then I can pretend that they are. – Sammitch Jan 04 '18 at 20:46
  • 1
    Why do you assign to `$PDO_ob` twice, before the `try` and again inside it? – Barmar Jan 04 '18 at 20:48
  • 1
    I would suggest to keep with global. There is no security issues with global, and for such a code it's all right to use it – Your Common Sense Jan 04 '18 at 20:49
  • 1
    It sounds like you need to review the chapter on functions in a PHP tutorial, it will explain how you pass parameters to functions. – Barmar Jan 04 '18 at 20:50
  • Bammer : assigning to $PDO_ob twice It was a mistake in copying the code – dumro Jan 04 '18 at 22:14
  • @Barmar Are you recommend any source to the chapter on functions in a PHP tutorial ,please – dumro Jan 04 '18 at 22:21
  • Sorry, I don't know tutorials to recommend. But wherever you're learning PHP from, it should explain it. – Barmar Jan 04 '18 at 22:25

1 Answers1

-2

In addition to the comments suggestions, I would suggest to use a Class for the database connection and use a get instance function.

final class DatabaseFactory
{
    public $pdo;
    public static function Instance()
    {
        static $inst = null;
        if ($inst === null) {
            $inst = new DatabaseFactory();
        }
        return $inst;
    }

    /**
     * Private ctor so nobody else can instantiate it
     *
     */
    private function __construct()
    {
         $dsn='mysql:host=localhost;dbname=sakila';
       $hostname='localhost';
       $username='root'; // avoid connecting with root user 
       $password='{put password for db connection, it is more secure}';
       try {
            $this->pdo = new PDO($dsn, $username, $password);
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              echo " connection success";
        } catch (PDOException $ex) {
            echo 'Connection failed: ' . $ex->getMessage();
        }
    }
}
  • Thank You I am still beginner and I want to learn the way to solve the problem without further copmlications – dumro Jan 04 '18 at 22:16
  • Thank You very Much But , I do not know how to make class from database yet – dumro Jan 04 '18 at 22:27
  • Singleton DB classes are a real bad idea. https://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons – Sammitch Jan 05 '18 at 00:57