This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// Prepare statement | |
$stmt = $db->prepare("SELECT user, email, country FROM users WHERE id > 1000"); | |
$stmt->execute(); | |
// Get results | |
if($stmt->rowCount() > 0) { | |
// One or more rows returned, start iteration through set | |
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// Get username | |
$userIDs = array(10, 38, 56, 93); | |
// Method 1: array_fill() then implode() | |
$placeholders = implode(',', array_fill(0, count($userIDs), '?')); | |
// Method 2: str_repeat() then str_split() then implode() | |
$placeholders = implode(',', str_split(str_repeat('?', count($userIDs)))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// You have an array of userdata | |
$userData = array([1] => 'johndoe', [2] => '[email protected]', [3] => 'USA'); | |
// Prepare statement | |
$stmt = $db->prepare("INSERT INTO users (user, email, country) VALUES (?, ?, ?)"); | |
// For loop | |
// Bind param binds the REFERENCE, remember to modify array (&$value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// Get username | |
$username = 'john'; | |
$id = 1000; | |
// Using named placeholders | |
$stmt = $db->prepare("SELECT user, email, country FROM users WHERE user LIKE :username AND id > :id") | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// Some variables | |
$userid = 1000; | |
$country = 'USA'; | |
// Using named placeholders | |
$stmt = $db->prepare("SELECT user, email, country FROM users WHERE id > :id AND country = :country") | |
// The order of objects in the array does not matter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// Get username | |
$username = 'johndoe'; | |
$stmt = $db->prepare("SELECT user, email, country FROM users WHERE user = :username"); | |
$stmt->bindParam(':username', $username); | |
$username = 'janesmith'; | |
$stmt->execute(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// Receive user input that is separated by a new line for each row | |
$favouriteFoods = array('Apple', 'Poutine', 'Cheesestrings', 'Pickle chips', 'Peanut butter and raisins'); | |
$user = 'johndoe'; | |
// Prepare statement | |
$stmt = $db->prepare("INSERT INTO users (user, foodItem) VALUES(:username, :favouriteFood)"); | |
$db->beginTransaction(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// Let's say a user is allowed to provide a table name to query from | |
$tableName = trim($_GET['tableName']); | |
// This would NOT work (no good!) | |
$stmt = $db->prepare("SELECT user, id, email FROM :table WHERE id > 1000"); | |
$stmt->bindParam(':table', $tableName); | |
$stmt->execute(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
// Open new connection, allow catching of exceptions | |
$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS); | |
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
// Wrap the rest of your code in the 'try' block | |
// since any step in here can go wrong, and you | |
// will be able to catch any exceptions. | |
} catch (PDOException $e) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Assuming that database connection is already open | |
// Prepare statement | |
$stmt = $db->prepare("SELECT | |
COUNT(user_id) AS UserCount, | |
country AS Country | |
FROM users | |
GROUP BY country | |
ORDER BY country"); | |