Sep 13, 2018

MySQL Prepared statement Examples

Java JDBC
This example uses Java and the JDBC API:
java.sql.PreparedStatement stmt = connection.prepareStatement(
               "SELECT * FROM users WHERE USERNAME = ? AND ROOM = ?");
stmt.setString(1, username);
stmt.setInt(2, roomNumber);
stmt.executeQuery();
Java PreparedStatement provides "setters" (setInt(int), setString(String), setDouble(double), etc.) for all major built-in data types.
PHP PDO
This example uses PHP and PHP Data Objects (PDO):

$stmt = $dbh->prepare("SELECT * FROM users WHERE USERNAME = ? AND PASSWORD = ?");
$stmt->execute(array($username, $password));

MySQL
PREPARE i88_ca FROM
 'insert low_priority ignore into 
sharedlist(listid, contactid) 
select ?, sl.contactid  from sharedlist sl 
join contacts c on c.contactid=sl.contactid 
where sl.listid=? order by c.email limit ?';

EXECUTE i88_ca USING @v_listid1,@v_listid,@count;