update page now

Voting

: max(two, two)?
(Example: nine)

The Note You're Voting On

esundberg at nitelusa dot com
5 years ago
Working PDO Prepare and Execute Example

Code
----------------------------------------
print "<h1>PDO Example</h1>";

print "<h2>PDO Connection</h2>";
try {
    $pdo = new PDO("sqlsrv:server=$sql_server;Database=$sql_database",$sql_username,$sql_password,['ReturnDatesAsStrings'=>true]);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
    die("Database Connection Error");
    
}

print "<h2>Check for PDO Connection</h2>";
if($pdo === false) {
    print "No DB Connection<br>";
} else {
    print "Good DB Connection<br>";
}

print "<h2>PDO Query Example 1 with SQL Injection</h2>";
print "I Personally prefer pdo due to binding of paramaters by name.<br>";
$sql = "SELECT username, active FROM users WHERE username = :username";
print "SQL: $sql\n";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':username', $username);
$stmt->execute();
while($r = $stmt->fetch(PDO::FETCH_OBJ)) {
    print_r($r);
}

------------------------------------------------------
PDO Example
PDO Connection
Check for PDO Connection
Good DB Connection
PDO Query Example 1 with SQL Injection
I Personally prefer pdo due to binding of paramaters by name.
SQL: SELECT username, active FROM users WHERE username = :username
stdClass Object
(
    [username] => admin
    [active] => 1
)

<< Back to user notes page

To Top