Little Sql-Class


SUBMITTED BY: Guest

DATE: June 14, 2013, 12:10 p.m.

FORMAT: PHP

SIZE: 2.0 kB

HITS: 5232

  1. public function GetSpecifiedData($tableName, $atributeName, $value)
  2. {
  3. $sqlConntect = new mysqli(SERVER, USERNAME, PASSWORD, DATABASE);
  4. if(!mysqli_connect_errno())
  5. {
  6. $atributes = null;
  7. foreach ($atributeName as $atributeTemp)
  8. $atributes .= sprintf("%s,", $atributeTemp);
  9. $atributes = substr($atributes, 0, -1);
  10. $values = null;
  11. if($value != null)
  12. {
  13. foreach ($value as $valueTemp)
  14. $values .= sprintf("'%s', ", $valueTemp);
  15. $values = substr($values, 0, -2);
  16. }
  17. $sqlCommand = null;
  18. if($atributeName != null && $value != null)
  19. $sqlCommand = sprintf("SELECT %s FROM %s WHERE %s", $atributes, $tableName, $values);
  20. if($atributeName == null && $value != null)
  21. $sqlCommand = sprintf("SELECT * FROM %s WHERE %s", $tableName, $values);
  22. if($atributeName != null && $value == null)
  23. $sqlCommand = sprintf("SELECT %s FROM %s", $atributes, $tableName);
  24. if($atributeName == null && $value == null)
  25. $sqlCommand = sprintf("SELECT * FROM %s", $tableName);
  26. if($sqlCommand != null)
  27. {
  28. $output = array();
  29. if($sqlResult = $sqlConntect->query($sqlCommand))
  30. {
  31. while ($sqlRow = $sqlResult->fetch_row()) {
  32. $output[] = $sqlRow;
  33. }
  34. }
  35. return $output;
  36. }
  37. }
  38. else
  39. echo sprintf("Connection to Database Faild. Message: %s", mysqli_connect_error());
  40. return null;
  41. }

comments powered by Disqus