note 124804 added to function.sqlsrv-execute

From: Date: Sun, 08 Mar 2020 06:22:54 +0000
Subject: note 124804 added to function.sqlsrv-execute
Groups: php.notes 
Request: Send a blank email to php-notes+get-214300@lists.php.net to get a copy of this message
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 ) ---- Server IP: 45.112.84.4 Probable Submitter: 38.142.137.34 (proxied: 73.22.78.74) ---- Manual Page -- http://php.net/manual/en/function.sqlsrv-execute.php Edit -- https://master.php.net/note/edit/124804 Del: integrated -- https://master.php.net/note/delete/124804/integrated Del: useless -- https://master.php.net/note/delete/124804/useless Del: bad code -- https://master.php.net/note/delete/124804/bad+code Del: spam -- https://master.php.net/note/delete/124804/spam Del: non-english -- https://master.php.net/note/delete/124804/non-english Del: in docs -- https://master.php.net/note/delete/124804/in+docs Del: other reasons-- https://master.php.net/note/delete/124804 Reject -- https://master.php.net/note/reject/124804 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214300) next »