Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all,
Vanilla are planning an update to the site on April 24th (next Wednesday). It is a major PHP8 update which is expected to boost performance across the site. The site will be down from 7pm and it is expected to take about an hour to complete. We appreciate your patience during the update.
Thanks all.

PDO scope issue

  • 28-05-2014 6:20pm
    #1
    Registered Users Posts: 263 ✭✭


    Hi guys quick question.

    Just trying to implement the PDO object in PHP - but alas I hit a wall. I liek to keep all the functions in a seperate file called functions and it will have functions like checkuser() updateuser() deleteuser() - these exist in the functions.php. When creating the PDO object I create it in connect.php (so as If i need to change the db config moving the site)

    HOWEVER!!! the pdo object is not available in the scope of the checkuser() updateuser() deleteuser() in functions.php file. It seems it is outside the scope.

    Previously my function would be :

    function loadCaseInfo($va1, $var2)
    {
    $sqlQuery = "SELECT * from Record ";
    $result = mysql_query($sqlQuery);
    if (!$result) die("Error: " .mysql_error());
    if (mysql_num_rows($result)>=1) {
    $memberArray = mysql_fetch_assoc($result);
    return $memberArray;
    }

    The mysql_query was available across the board which made it simple. The PDO object however is not and I have been trying all sorts of ways to make it work like :

    class getDB {
    function createDB(){
    $handler = new PDO('mysql:'.DB_PDO_DB,DB_USER,DB_PASSWORD);
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    return $handler;
    }
    }

    $db = new getDB();
    $statement = $db->createDB();
    $record = $statement->prepare("select * from users ");

    Im pretty sure this is the complete wrong way about going about it and want to know the "correct way" of dealing with PDO objects. I have tried to call print_r($handler) in checkuser() updateuser() deleteuser() but comes up blank however if I call it outside of these functiosn but within the functions.php file (which houses all these functions) it works?!?!?!? any ideas??

    B


Comments

  • Registered Users Posts: 263 ✭✭swordsinfo


    ok so I have found a work around by using the global statement which makes the pdo object available in the function. Follow up code similar to

    defined('xxxxx') or die('Restricted access');
    include_once (APPLICATION_PATH . "/inc/db.inc.php"); //pdo connection defined as $handler

    function authenticate($username, $password) {
    global $handler; // imports the $handler pdo object and makes available for db access
    print_r($handler);
    if (empty($username) or empty($password)){
    header("Location: index.php?action=fieldserror");
    }

    My question still stands should I have global $handler defined in each of my functions or am I looking at this problem the wrong way round. I have been looking at classes as an alternative option but I still think I would have to create a new object each time I wanted to run one of the class functions?? Any suggestions I am at the wall at this stage


  • Registered Users Posts: 5,994 ✭✭✭Talisman


    Read up on Autoloading Classes.

    You don't need to include the class everywhere because PHP5 introduced a magic function __autoload(), it gets called when you attempt to use a class or interface that has not yet been loaded. This is used widely in all the PHP frameworks.

    You also might benefit from reading up on these:

    You could create the database class as a Singleton.

    Alternatively you could use Dependency Injection.


  • Registered Users Posts: 263 ✭✭swordsinfo


    Talisman wrote: »
    Read up on Autoloading Classes.

    You don't need to include the class everywhere because PHP5 introduced a magic function __autoload(), it gets called when you attempt to use a class or interface that has not yet been loaded. This is used widely in all the PHP frameworks.

    You also might benefit from reading up on these:

    You could create the database class as a Singleton.

    Alternatively you could use Dependency Injection.

    I have seen singletons being mentioned previously but how does that fit with my issue of scope problems?


  • Registered Users Posts: 5,994 ✭✭✭Talisman


    The singleton pattern is used to ensure there is only one instance of a class, and that global access is possible.

    How To Code A Singleton Design Pattern In PHP 5+


  • Registered Users Posts: 263 ✭✭swordsinfo


    So classes are the "best" way to utilise the PDO object? If this is the case then I might need to top up on my OOP knowlegde first before utilising classes?


  • Advertisement
  • Registered Users Posts: 5,994 ✭✭✭Talisman


    OOP is the best approach to dealing with data. An object can store data internally so variables don't need to be passed from function to function in order to work properly.


  • Registered Users Posts: 263 ✭✭swordsinfo


    Appreciate the help - was at a wall with its approach - I came across this which may help users with class oop with PDO.

    <code>www.imavex.com/php-pdo-wrapper-class/</code>

    Thanks again and happy Friday!


Advertisement