1. Go to this page and download the library: Download dealnews/db 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/ */
dealnews / db example snippets
$mydb = \DealNews\DB\Factory::init("mydb");
$crud = \DealNews\DB\CRUD::factory('mydb');
// Create
$result = $crud->create(
// table name
"test",
// data to add
[
"name" => $name,
"description" => $description,
]
);
// Read
$rows = $crud->read(
// table name
"test",
// where clause data
["id" => $id]
);
// Update
$result = $crud->update(
// table name
"test",
// data to update
["name" => "Test"],
// where clause data
["id" => $id]
);
// Delete
$result = $crud->delete(
// table name
"test",
// where clause data
["id" => $row["id"]]
);
// Run a select with no parameters
$stmt = $crud->run("select * from table limit 10");
// Run a select query with paramters
$stmt = $crud->run(
"select * from table where foo = :foo"
[
":foo" => $foo
]
);
// value object
class Book {
public string $title = '';
public string $author = '';
public string $isbn = '';
}
// mapper
class BookMapper extends \DealNews\DB\AbstractMapper {
public const DATABASE_NAME = 'example';
public const TABLE = 'books';
public const PRIMARY_KEY = 'isbn';
public const MAPPED_CLASS = Book::class;
public const MAPPING = [
'title' => [],
'author' => [],
'isbn' => []
];
}
$book = new Book();
$book->title = 'Professional PHP Programming';
$book->author = 'Jesus Castagnetto';
$book->isbn = '1-861002-96-3';
$mapper = new BookMapper();
$book = $mapper->save($book); // the entity is returned from save, reloaded from the database
// load a book
$book = $mapper->load('1-861002-96-3');
// delete a book
$mapper->delete('1-861002-96-3');
// find books based on author
$books = $mapper->find(['author' => 'Rasmus Lerdorf'], limit: 10, start: 0, order: 'title');