PHP code example of lodev09 / php-models

1. Go to this page and download the library: Download lodev09/php-models library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lodev09 / php-models example snippets


// connect to your database. Store the $db instance globally -- you only need to connect to your db ONCE!
$db = new \Models\DB(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);

$users = $db->select("SELECT * FROM users WHERE active = 1 AND username = :username", array('username' => 'lodev09'));
var_dump($users);

$db = new \Models\DB(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);
\Models\Model::setDb($db);

namespace Models;

class User extends Model {
    public function getName() {
        return $this->name;
    }
}

// somewhere in your init.php
\Models\User::register('users');

$user = \Models\User::row("SELECT id, name FROM users WHERE id = 1 AND active = 1");
// you can call the getName() method now
if ($user) {
    $name = $user->getName();
    echo 'His name is '.$name;
}
term
$ composer