Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env php
- <?php
- $short_opts = 'u:p:q:';
- $long_opts = ['user:', 'password:', 'query:'];
- $args = getopt($short_opts, $long_opts);
- $user = $args['u'] ?? $args['user'];
- $password = $args['p'] ?? $args['password'];
- $query = $args['q'] ?? $args['query'];
- $query = "%$query%";
- $notices = [];
- try {
- $mysqli = new mysqli('localhost', $user, $password, 'social');
- } catch (Throwable $e) {
- die("Error while establising connection to MySQL: {$e->getMessage()}");
- }
- try {
- $stmt = $mysqli->prepare("SELECT id, url, content FROM notice WHERE content LIKE ?");
- $stmt->bind_param('s', $query);
- $stmt->execute();
- $result = $stmt->get_result();
- if ($result->num_rows > 0) {
- while ($row = $result->fetch_assoc()) {
- $notices[] = ['id' => $row['id'], 'url' => $row['url'], 'content' => $row['content']];
- }
- }
- } catch (Throwable $e) {
- print "Error while executing query: {$e->getMessage()}\n";
- } finally {
- $stmt->close();
- }
- $mysqli->close();
- foreach ($notices as $notice) {
- print "ID: {$notice['id']}\n";
- print "URL: {$notice['url']}\n";
- print $notice['content'] . "\n";
- print "-----------------------------------------\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement