Use $db in external page

Use $db in external page

marianidiegomarianidiego Posts: 12Questions: 4Answers: 0

Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:

I am trying to create the first pages with datables.

Like any site, I need a login, and would like to create a page for importing data from an external JSON.

Since database access has already been defined in my datatables, and includes a class that handles different types of databases.... I would like to use this class for my login and import function (Database.php).

Functions as:
"$mysqli->prepare("SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1")) "

How do I use this datatables package class for external applications from datatables (login and import)?

Could you, kindly, give me a small example?

Answers

  • allanallan Posts: 54,572Questions: 1Answers: 8,534 Site admin

    Sure! The reference documentation for the class is here.

    You’d include the Datatables.php file like normal, which will get you that magic $db variable automatically, then you can do:

    $row = $db
      ->select([‘id’, ‘username’, ...], ‘members’, [‘email’ => $email])
      ->fetch();
    

    With a limit you need to build the query more expilicity:

    $row = $db
      ->query( 'select' )
      ->table( ‘members’ )
      ->get( [‘id’, ‘username’, ... )
      ->where([‘email’ => $email] )
      ->limit( 1 )
      ->exec()
      ->fetch();
    

    Allan

  • marianidiegomarianidiego Posts: 12Questions: 4Answers: 0

    My script is now:

    <?php
    
    // DataTables PHP library and database connection
    include( "class/Editor-PHP/lib/DataTables.php" );
    
    
    // Alias Editor classes so they are easy to use
    use
        DataTables\Database,
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    
    function login($email, $password) {
    
       $row = $db->select ( "examiners", "*", "email=" . $email )->fetch();
    
        print_r($row);
    }
    
    ?>
    

    but return this error:

    Notice: Undefined variable: db

  • marianidiegomarianidiego Posts: 12Questions: 4Answers: 0
    edited May 21

    I've been testing, I'm going crazy because I don't understand why:

    If I write like this IT WORKS:

    <?php
    
    // DataTables PHP library and database connection
    include( "class/Editor-PHP/lib/DataTables.php" );
    
    
    // Alias Editor classes so they are easy to use
    use DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
       $row = $database->select ( "examiners", "*", array("email"=>"mariani.diego@gmail.com") )->fetch();
    
       print_r($row);    
    function make_login($email, $password) {}
    

    But if I write like this it does NOT work:

    <?php
    
    // DataTables PHP library and database connection
    include( "class/Editor-PHP/lib/DataTables.php" );
    
    
    // Alias Editor classes so they are easy to use
    use DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
    function make_login($email, $password) {
       $row = $database->select ( "examiners", "*", array("email"=>"mariani.diego@gmail.com") )->fetch();
    
       print_r($row);}  
    

    Why?!?!!?!

  • rf1234rf1234 Posts: 1,848Questions: 55Answers: 279
    edited May 21

    If you don't pass the db handler into your function it can't be there ... The db handler is a global variable.

    <?php
     
    // DataTables PHP library and database connection
    include( "class/Editor-PHP/lib/DataTables.php" );
     
     
    // Alias Editor classes so they are easy to use
    use
        DataTables\Database,
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
     
    
    login('your@email.com', 'blablabla', $db);
     
    function login($email, $password, $db) {
     
       $row = $db->select ( "examiners", "*", "email=" . $email )->fetch();
     
        print_r($row);
    }
     
    ?>
    

    Alternatively you could write this:

    function login($email, $password) {
        global $db;
     
       $row = $db->select ( "examiners", "*", "email=" . $email )->fetch();
     
        print_r($row);
    }
    
  • allanallan Posts: 54,572Questions: 1Answers: 8,534 Site admin

    A final option is:

    function login($email, $password) use ($db) {
      ...
    }
    

    You have to remember that PHP doesn't scope its variables like Javascript does. In PHP they are locally scoped by default.

    Allan

Sign In or Register to comment.