Advertisement
Guest User

Untitled

a guest
May 15th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #!/usr/bin/env php
  2. <?php
  3.  
  4. $short_opts = 'u:p:q:';
  5. $long_opts = ['user:', 'password:', 'query:'];
  6. $args = getopt($short_opts, $long_opts);
  7.  
  8. $user = $args['u'] ?? $args['user'];
  9. $password = $args['p'] ?? $args['password'];
  10. $query = $args['q'] ?? $args['query'];
  11. $query = "%$query%";
  12.  
  13. $notices = [];
  14.  
  15. try {
  16. $mysqli = new mysqli('localhost', $user, $password, 'social');
  17. } catch (Throwable $e) {
  18. die("Error while establising connection to MySQL: {$e->getMessage()}");
  19. }
  20.  
  21. try {
  22. $stmt = $mysqli->prepare("SELECT id, url, content FROM notice WHERE content LIKE ?");
  23. $stmt->bind_param('s', $query);
  24. $stmt->execute();
  25. $result = $stmt->get_result();
  26. if ($result->num_rows > 0) {
  27. while ($row = $result->fetch_assoc()) {
  28. $notices[] = ['id' => $row['id'], 'url' => $row['url'], 'content' => $row['content']];
  29. }
  30. }
  31. } catch (Throwable $e) {
  32. print "Error while executing query: {$e->getMessage()}\n";
  33. } finally {
  34. $stmt->close();
  35. }
  36. $mysqli->close();
  37.  
  38. foreach ($notices as $notice) {
  39. print "ID: {$notice['id']}\n";
  40. print "URL: {$notice['url']}\n";
  41. print $notice['content'] . "\n";
  42. print "-----------------------------------------\n";
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement