Guest User

Untitled

a guest
Oct 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. // Perform a MySQL Query.
  2. function query($what,$from,$where = NULL,$orderBy = NULL) {
  3.  
  4. // Create the beginning string.
  5. $select = 'SELECT '.$what.' FROM '.$from.' ';
  6.  
  7. // Set a variable to 0.
  8. $i = 1;
  9.  
  10. // If there ar where statements...
  11. if(!empty($where)) {
  12.  
  13. // Add WHERE to the select.
  14. $select .= "WHERE ";
  15.  
  16. // For every WHERE statement...
  17. foreach($where as $key => $value) {
  18.  
  19. // Add on to the string.
  20. $select .= $key.'="'.$value.'" ';
  21.  
  22. // Get the number of array objects.
  23. $count = count($where);
  24.  
  25. // If the count is not the last item in the array...
  26. if($i < $count) {
  27.  
  28. // Then add an AND!
  29. $select .= ' AND ';
  30.  
  31. }
  32.  
  33. // Increase i by 1.
  34. $i++;
  35.  
  36. }
  37.  
  38. }
  39.  
  40. // If there is an order...
  41. if(!empty($orderBy)) {
  42.  
  43. $select .= "ORDER BY ".$orderBy;
  44.  
  45. }
  46.  
  47. // Filter the select.
  48. $selectFiltered = mysql_real_escape_string($select);
  49.  
  50. // Attempt the query.
  51. $query = mysql_query($select);
  52.  
  53. // If it fails...
  54. if(!$query) {
  55.  
  56. // Output an error.
  57. $this->primary->errorMsg('MySQL Error','A MySQL Error occured trying to process the query "'.$selectFiltered.'". The system cannot continue.');
  58.  
  59. }
  60.  
  61. return $query;
  62.  
  63. }
Add Comment
Please, Sign In to add comment