Skip to content

Commit 15afc2e

Browse files
authored
Examples
1 parent 5feb4e3 commit 15afc2e

12 files changed

+187
-0
lines changed

Diff for: examples/avg.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
// Define the WHERE clause and bindings
7+
$where = 'country = ?';
8+
$bindings = array('USA');
9+
10+
// Calculate the average and output it
11+
$avg = $db->avg('customers', 'age', $where, $bindings);
12+
echo "The average age of customers from the USA is $avg";

Diff for: examples/count.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
// Define the WHERE clause and bindings
7+
$where = 'id = ?';
8+
$bindings = array('1');
9+
10+
// Get the count and output it
11+
$count = $db->count('users', $where, $bindings);
12+
echo "There are $count active users";

Diff for: examples/delete.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
// Define the WHERE clause and bindings
7+
$where = 'id = ?';
8+
$bindings = array(1);
9+
10+
// Perform the delete and check if it was successful
11+
$result = $db->delete('users', $where, $bindings);
12+
if ($result) {
13+
echo 'Delete successful';
14+
} else {
15+
echo 'Delete failed';
16+
}

Diff for: examples/get.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
// Get the row with ID 4 from the users table
7+
$row = $db->get('users', 4);
8+
9+
// Output the values of the row
10+
echo "Name: {$row['name']}, Email: {$row['email']}";

Diff for: examples/insert.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
$data = array(
6+
'name' => 'John Doe',
7+
'email' => '[email protected]',
8+
'password' => 'mypass'
9+
);
10+
$result = $db->insert('users', $data);
11+
if ($result) {
12+
echo 'New user added successfully.';
13+
} else {
14+
echo 'Error adding new user.';
15+
}

Diff for: examples/limit-pages.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
// Set up pagination parameters
7+
$perPage = 10;
8+
$page = isset($_GET['page']) ? $_GET['page'] : 1;
9+
$offset = ($page - 1) * $perPage;
10+
11+
// Define the conditions and options for the query
12+
$conditions = array();
13+
$options = array(
14+
'LIMIT' => "$offset, $perPage"
15+
);
16+
17+
// Perform the query and retrieve the results
18+
$results = $db->select('users', '*', $conditions, $options);
19+
20+
// Count the total number of results
21+
$totalResults = count($db->select('users'));
22+
23+
// Calculate the total number of pages
24+
$totalPages = ceil($totalResults / $perPage);
25+
26+
// Output the results and pagination links
27+
foreach ($results as $result) {
28+
echo $result['id'] . ': ' . $result['name'] . '<br>';
29+
}
30+
31+
echo '<br>';
32+
33+
if ($totalPages > 1) {
34+
echo 'Pages: ';
35+
for ($i = 1; $i <= $totalPages; $i++) {
36+
if ($i == $page) {
37+
echo "$i ";
38+
} else {
39+
echo "<a href=\"?page=$i\">$i</a> ";
40+
}
41+
}
42+
}

Diff for: examples/limit.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
$options = array(
7+
'LIMIT' => '2'
8+
);
9+
10+
$results = $db->select('users', '*', array(), $options);
11+
12+
print_r($results);

Diff for: examples/min-max.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
// Define the WHERE clause and bindings
7+
$where = 'name = ?';
8+
$bindings = array('John Doe');
9+
10+
// Find the minimum id and output it
11+
$minID = $db->min('users', 'id', $where, $bindings);
12+
echo "The minimum id of John Doe name is $minID <br />";
13+
14+
// Find the maximum id and output it
15+
$maxID = $db->max('users', 'id', $where, $bindings);
16+
echo "The maximum id of John Doe name is $maxID";

Diff for: examples/query.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
// Define the SQL query and bindings
7+
$sql = "SELECT * FROM customers WHERE country = ?";
8+
$bindings = array('USA');
9+
10+
// Run the query and output the results
11+
$results = $db->query($sql, $bindings);
12+
foreach ($results as $row) {
13+
echo "Name: {$row['name']}, Age: {$row['age']}, Email: {$row['email']}<br>";
14+
}

Diff for: examples/search.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
$users = $db->select('users', '*', ['name' => ['LIKE' => 'H']]);
7+
foreach ($users as $user) {
8+
echo $user['id'] . ': ' . $user['name'] . ' - ' . $user['email'] . '<br>';
9+
}

Diff for: examples/selectAll.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
$users = $db->select('users');
7+
foreach ($users as $user) {
8+
echo $user['id'] . ': ' . $user['name'] . ' - ' . $user['email'] . '<br>';
9+
}

Diff for: examples/update.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require_once 'EasySQL.php';
3+
4+
$db = new EasySQL('localhost', 'Easy', 'root', '123456');
5+
6+
// Define the data to be updated and the WHERE clause
7+
$data = array(
8+
'name' => 'John Doe',
9+
'email' => '[email protected]'
10+
);
11+
$where = 'id = ?';
12+
$bindings = array(1);
13+
14+
// Perform the update and check if it was successful
15+
$result = $db->update('users', $data, $where, $bindings);
16+
if ($result) {
17+
echo 'Update successful';
18+
} else {
19+
echo 'Update failed';
20+
}

0 commit comments

Comments
 (0)