-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb00102
commit 2ae1c08
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
$inData = getRequestInfo(); | ||
$searchResults = ""; | ||
$searchCount = 0; | ||
|
||
|
||
|
||
$conn = new mysqli("localhost", "TheBeast", "WeLoveCOP4331", "COP4331"); | ||
if ($conn->connect_error) | ||
{ | ||
returnWithError( $conn->connect_error ); | ||
} | ||
else | ||
{ | ||
$stmt = $conn->prepare("SELECT * FROM Contacts WHERE (FirstName LIKE ? OR LastName LIKE ? ) AND CreatedByUserID=?"); | ||
$searchName = "%" . $inData["search"] . "%"; | ||
$stmt->bind_param("ssi", $searchName, $searchName, $inData["createdByUserId"]); | ||
$stmt->execute(); | ||
|
||
$result = $stmt->get_result(); | ||
|
||
while($row = $result->fetch_assoc()) | ||
{ | ||
if( $searchCount > 0 ) | ||
{ | ||
$searchResults .= ","; | ||
} | ||
$searchCount++; | ||
|
||
$searchResults .= '{"FirstName" : "' . $row["FirstName"]. '", "LastName" : "' . $row["LastName"]. '", "EmailAddress" : "' . $row["EmailAddress"]. '", "PhoneNumber" : "' . $row["PhoneNumber"]. '", "UserID" : "' . $row["UserID"].'", "ID" : "' . $row["ID"]. '"}'; | ||
} | ||
|
||
if( $searchCount == 0 ) | ||
{ | ||
returnWithError( "No Records Found" ); | ||
} | ||
else | ||
{ | ||
returnWithInfo( $searchResults ); | ||
} | ||
|
||
$stmt->close(); | ||
$conn->close(); | ||
} | ||
|
||
function getRequestInfo() | ||
{ | ||
return json_decode(file_get_contents('php://input'), true); | ||
} | ||
|
||
function sendResultInfoAsJson( $obj ) | ||
{ | ||
header('Content-type: application/json'); | ||
echo $obj; | ||
} | ||
|
||
function returnWithError( $err ) | ||
{ | ||
$retValue = '{"id":0,"firstName":"","lastName":"","error":"' . $err . '"}'; | ||
sendResultInfoAsJson( $retValue ); | ||
} | ||
|
||
function returnWithInfo( $searchResults ) | ||
{ | ||
$retValue = '{"results":[' . $searchResults . '],"error":""}'; | ||
sendResultInfoAsJson( $retValue ); | ||
} | ||
|
||
?> |