Skip to content

Commit 8913264

Browse files
committed
mysql select and filter data from database with php and pdo tutorial
1 parent 2e71dec commit 8913264

12 files changed

Lines changed: 752 additions & 0 deletions

File tree

mysql_php_pdo/and.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
// include global things for use
3+
include 'global_data.php';
4+
5+
// sql string
6+
$sql = '
7+
SELECT
8+
*
9+
FROM
10+
drinks
11+
WHERE
12+
size = :size AND
13+
sugar_free = :sugar_free
14+
';
15+
16+
$params = array( // keys must match the variables in the sql so they can get replaced with the value
17+
':size' => '8oz',
18+
':sugar_free' => '1'
19+
);
20+
21+
// prepare the sql
22+
$statement = $database->prepare( $sql );
23+
24+
// execute the sql
25+
$statement->execute( $params );
26+
27+
// get the query data
28+
$queryDrinks = $statement->fetchAll();
29+
30+
// display the html
31+
displayViewHtml( $sql, $allDrinks, $queryDrinks, $statement );
32+
?>

mysql_php_pdo/between.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
// include global things for use
3+
include 'global_data.php';
4+
5+
$sql = array( // array keyed by the various sql statments we can run
6+
'BETWEEN' => '
7+
SELECT
8+
*
9+
FROM
10+
drinks
11+
WHERE
12+
expiration_date BETWEEN
13+
CAST(:start_date AS DATE) AND
14+
CAST(:end_date AS DATE)
15+
',
16+
'NOTBETWEEN' => '
17+
SELECT
18+
*
19+
FROM
20+
drinks
21+
WHERE
22+
expiration_date NOT BETWEEN
23+
CAST(:start_date AS DATE) AND
24+
CAST(:end_date AS DATE)
25+
'
26+
);
27+
28+
// check url for sql statement to run
29+
$sql = isset( $_GET['filter'] ) && isset( $sql[$_GET['filter']] ) ? $sql[$_GET['filter']] : $sql['BETWEEN'];
30+
31+
$params = array( // keys must match the variables in the sql so they can get replaced with the value
32+
':start_date' => '2021-11-01',
33+
':end_date' => '2021-12-01'
34+
);
35+
36+
// prepare the sql
37+
$statement = $database->prepare( $sql );
38+
39+
// execute the sql
40+
$statement->execute( $params );
41+
42+
// get the query data
43+
$queryDrinks = $statement->fetchAll();
44+
45+
// display the html
46+
displayViewHtml( $sql, $allDrinks, $queryDrinks, $statement );
47+
?>

mysql_php_pdo/distinct.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
// include global things for use
3+
include 'global_data.php';
4+
5+
// sql string
6+
$sql = '
7+
SELECT DISTINCT
8+
name,
9+
flavor,
10+
size
11+
FROM
12+
drinks
13+
WHERE
14+
size = :size OR
15+
flavor = :flavor
16+
';
17+
18+
$params = array( // keys must match the variables in the sql so they can get replaced with the value
19+
':size' => '8oz',
20+
':flavor' => 'Tropical'
21+
);
22+
23+
// prepare the sql
24+
$statement = $database->prepare( $sql );
25+
26+
// execute the sql
27+
$statement->execute( $params );
28+
29+
// get the query data
30+
$queryDrinks = $statement->fetchAll();
31+
32+
// generate page title
33+
$pageTitle = 'MySQL SELECT ' . strtoupper( pathinfo( $_SERVER['SCRIPT_FILENAME'], PATHINFO_FILENAME ) ) . ' | PHP Tutorial | PDO Tutorial';
34+
?>
35+
<!DOCTYPE html>
36+
<html>
37+
<head>
38+
<title>
39+
<?php echo $pageTitle; // display page title ?>
40+
</title>
41+
</head>
42+
<body>
43+
<?php displayNavBar(); // display the navigation bar ?>
44+
<h1>
45+
<?php echo $pageTitle; // display page title ?>
46+
</h1>
47+
<hr />
48+
<?php displayDrinks( $allDrinks ); // display all drinks in a table ?>
49+
<hr />
50+
<div>
51+
<h2>
52+
<?php echo $sql; // display the sql statement we are running ?>
53+
</h2>
54+
<table border="1">
55+
<tr>
56+
<th>name</th>
57+
<th>flavor</th>
58+
<th>size</th>
59+
</tr>
60+
<?php foreach ( $queryDrinks as $drink ) : // loop over the data and display each row ?>
61+
<tr>
62+
<td><?php echo $drink['name']; ?></td>
63+
<td><?php echo $drink['flavor']; ?></td>
64+
<td><?php echo $drink['size']; ?></td>
65+
</tr>
66+
<?php endforeach; ?>
67+
</table>
68+
<h2>
69+
PDO Debug
70+
</h2>
71+
<pre><?php $statement->debugDumpParams(); // dump out pdo params ?></pre>
72+
<h2>
73+
Results
74+
</h2>
75+
<pre><?php print_r( $drinks ); // print out the raw php result array ?></pre>
76+
</div>
77+
</body>
78+
</html>

0 commit comments

Comments
 (0)