### Database Connection Examples Source: https://github.com/flightphp/active-record/blob/master/README.md Examples of how to establish database connections using PDO for SQLite and MySQL, and using mysqli. ```php // for sqlite $database_connection = new PDO('sqlite:test.db'); // this is just for example, you'd probably use a real database connection ``` ```php // for mysql $database_connection = new PDO('mysql:host=localhost;dbname=test_db&charset=utf8bm4', 'username', 'password'); ``` ```php // or mysqli $database_connection = new mysqli('localhost', 'username', 'password', 'test_db'); ``` ```php // or mysqli with non-object based creation $database_connection = mysqli_connect('localhost', 'username', 'password', 'test_db'); ``` -------------------------------- ### Install FlightPHP Active Record Source: https://github.com/flightphp/active-record/blob/master/README.md Install the FlightPHP Active Record package using Composer by running the provided command in your project directory. ```bash composer require flightphp/active-record ``` -------------------------------- ### Find Users by Name Pattern Source: https://github.com/flightphp/active-record/blob/master/README.md Retrieve user records that match a specific pattern in their name using the like() method followed by findAll(). ```php $users = $user->like('name', '%mamma%')->findAll(); ``` -------------------------------- ### Find All Users Source: https://github.com/flightphp/active-record/blob/master/README.md Retrieve all user records from the database using the findAll() method. ```php $users = $user->findAll(); ``` -------------------------------- ### Insert New User Record Source: https://github.com/flightphp/active-record/blob/master/README.md Create a new User object, set its properties, and insert it into the database using the insert() or save() method. The newly assigned ID can be accessed after insertion. ```php $user = new User($database_connection); $user->name = 'Bobby Tables'; $user->password = password_hash('some cool password'); $user->insert(); // or $user->save(); echo $user->id; // 1 $user->name = 'Joseph Mamma'; $user->password = password_hash('some cool password again!!!'); $user->insert(); echo $user->id; // 2 ``` -------------------------------- ### Define User ActiveRecord Class Source: https://github.com/flightphp/active-record/blob/master/README.md Define a User class that extends flight\ActiveRecord. It's recommended to add table column properties as comments. The constructor requires a database connection and the table name. ```php /** * An ActiveRecord class is usually singular * * It's highly recommended to add the properties of the table as comments here * * @property int $id * @property string $name * @property string $password */ class User extends flight\ActiveRecord { public function __construct($databaseConnection) { parent::__construct($databaseConnection, 'users', [/* custom values */]); } } ``` -------------------------------- ### Find User by ID Source: https://github.com/flightphp/active-record/blob/master/README.md Retrieve a user record from the database by its ID using the find() method. ```php $user->find(1); // find id = 1 in the database and return it. echo $user->name; // 'Bobby Tables' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.