Last active
May 10, 2018 19:56
-
-
Save hperrin/a5766b1167b07424080f to your computer and use it in GitHub Desktop.
Equivalent SQL Query from Frontend
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
function titleSeach (title, archived) { | |
return fetch( | |
'/api/titlesearch.php?title=' + | |
encodeURIComponent(title) + | |
'&archived=' + | |
encodeURIComponent(JSON.stringify(archived)) | |
).then(res => { | |
if (!res.ok) return Promise.reject(res); | |
return res.json() | |
}); | |
} | |
try { | |
const entities = await titleSearch('%not as easy%', false); | |
console.log(entities); | |
} catch (e) { | |
alert('Error'); | |
} |
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
<?php | |
// This is the endpoint for searching for a BlogPost | |
// by title pattern and archived flag. | |
$title = $_GET['title']; | |
$archived = ($_GET['archived'] === 'true' ? 'TRUE' : 'FALSE'); | |
$mysqli = new mysqli(); | |
$entities = []; | |
if ($stmt = $mysqli->prepare("SELECT * FROM BlogPosts WHERE title LIKE '?' AND archived=?")) { | |
$stmt->bind_param('ss', $title, $archived); | |
$stmt->execute(); | |
$result = $stmt->get_result(); | |
while ($row = $result->fetch_assoc()) { | |
$entities[] = $row; | |
} | |
$stmt->close(); | |
} | |
header('Content-Type: application/json'); | |
echo json_encode($entities); | |
$mysqli->close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment