note 124805 added to function.sqlsrv-query
| From: | erikpsundberg at gmail dot com | Date: | Sun, 08 Mar 2020 06:24:55 +0000 |
| Subject: | note 124805 added to function.sqlsrv-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214301@lists.php.net to get a copy of this message | ||
2 Working Example
print "<h1>SQL Query non PDO</h1>";
print "<h2>Connection</h2>";
$connectionInfo = array( "Database"=>$sql_database, "UID"=>$sql_username,
"PWD"=>$sql_password );
$conn = sqlsrv_connect( $sql_server, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
} else {
print "Good DB Connection: $conn<br>";
}
print "<h2>Query Example 1 | Fetching by Associate Array</h2>";
$sql = "SELECT username, active FROM users WHERE username = '$username'";
print "SQL: $sql\n";
$result = sqlsrv_query($conn, $sql);
if($result === false) {
die(print_r(sqlsrv_errors(), true));
}
#Fetching Data by array
while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
print_r($row);
}
print_line();
print "<h2>Query Example 2 | injection protection | Fetching by Object</h2>";
$sql = "SELECT username, active FROM users WHERE username = ?";
print "SQL: $sql\n";
$result = sqlsrv_query($conn, $sql, array($username));
if($result === false) {
die(print_r(sqlsrv_errors(), true));
}
#Fetching Data by object
while($row = sqlsrv_fetch_object($result)) {
print_r($row);
}
--------------------------------------------------
Result
Connection
Good DB Connection: Resource id #2
Query Example 1 | Fetching by Associate Array
SQL: SELECT username, active FROM users WHERE username = 'admin'
Array
(
[username] => admin
[active] => 1
)
######################################################################
Query Example 2 | injection protection | Fetching by Object
SQL: SELECT username, active FROM users WHERE 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-query.php
Edit -- https://master.php.net/note/edit/124805
Del: integrated -- https://master.php.net/note/delete/124805/integrated
Del: useless -- https://master.php.net/note/delete/124805/useless
Del: bad code -- https://master.php.net/note/delete/124805/bad+code
Del: spam -- https://master.php.net/note/delete/124805/spam
Del: non-english -- https://master.php.net/note/delete/124805/non-english
Del: in docs -- https://master.php.net/note/delete/124805/in+docs
Del: other reasons-- https://master.php.net/note/delete/124805
Reject -- https://master.php.net/note/reject/124805
Search -- https://master.php.net/manage/user-notes.php