Skip to content

Instantly share code, notes, and snippets.

@hperrin
Last active May 10, 2018 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hperrin/a5766b1167b07424080f to your computer and use it in GitHub Desktop.
Save hperrin/a5766b1167b07424080f to your computer and use it in GitHub Desktop.
Equivalent SQL Query from Frontend
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');
}
<?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