1. Go to this page and download the library: Download hnqca/database-php 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/ */
hnqca / database-php example snippets
nqca\Database\Connection;
use Hnqca\Database\Database;
/**
* Configures the database connection data:
*/
$connection = new Connection([
'driver' => 'mysql',
'host' => 'localhost',
'name' => 'your_database',
'user' => 'root',
'pass' => '',
'port' => '3306',
'charset' => 'utf8'
]);
try {
/**
* Initializes the database connection:
*/
$database = new Database($connection);
/**
* Handles the table in the currently set database during class instantiation:
*/
$results = $database->from('users')->select(true);
/**
* Displays the results found in the table:
*/
echo '<pre>';
print_r($results);
/***
* Checks if any error occurred during the process:
*/
} catch (\Exception $e) {
die('error: ' . $e->getMessage()); // Displays the reason for the error.
}
use Hnqca\Database\Connection;
use Hnqca\Database\Database;
/**
* Configures the database connection data:
*/
$connection = new Connection([
'driver' => 'mysql',
'host' => 'localhost',
'name' => 'your_database',
'user' => 'root',
'pass' => 'password',
'port' => '3306',
'charset' => 'utf8'
]);
/**
* Initializes the database connection:
*/
$database = new Database($connection);
/**
* Selecting multiple records:
*/
$users = $database->from('users')->select(true);
/**
* Limiting the columns to be selected:
*/
$users = $database->from('users')->select(true, ['id', 'age']);
/**
* Limiting the number of records to be selected:
*/
$users = $database->from('users')->limit(30)->select(true);
/**
* Selecting data from a specific user:
*/
$userId = 24;
$user = $database->from("users")->where("id = {$userId}")->select();
/**
* Selecting multiple records with one or more conditions:
*/
$name = "John";
$users = $database->from('users')->where("first_name = {$name}, age >= 18")->select(true);
/**
* You can also use "ORDER BY"
*/
$users = $database->from("users")->where("first_name = {$name}, age >= 18")->orderBy(['age' => 'DESC'])->select(true);
/**
* Displaying the obtained data:
*/
echo "Total users found: " . count($users);
foreach ($users as $user) {
echo "Id: {$user->id}" . '<br/>';
echo "Name: {$user->first_name} {$user->last_name}" . '<br/>';
echo '<hr />';
// ...
}
/**
* Delete specific records:
*/
$database->from('users')->where("id = 123")->delete();
/**
* Delete all records from the table. (be careful):
*/
$database->from('users')->delete();
/**
* Counting the number of records:
*/
$totalUsers = $database->from("users")->count();
/**
* Summing up some value from the column:
*/
$totalPrice = $database->from("products")->sum("price");
/**
* To calculate the average of values in a numeric column of a table.
*/
$averageAge = $database->from("users")->avg("age");
/**
* Finding the lowest value in the column:
*/
$lowestAge = $database->from("users")->min("age");
/**
* Finding the highest value in the column:
*/
$highestAge = $database->from("users")->max("age");
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.