database.php 785 B

1234567891011121314151617181920212223242526272829
  1. <?php
  2. require_once(realpath(dirname(__FILE__)).'/config.php');
  3. $mysqli = new mysqli(get('host'),get('user'),get('password'),get('database'));
  4. if($mysqli->connect_errno){
  5. echo "Failed to connect to MySQL: ".$mysqli->connect_error;
  6. }
  7. $mysqli->autocommit(true);
  8. function query($query,$args=Array()){
  9. global $mysqli;
  10. for ($i=0;$i<count($args);$i++){
  11. if(is_string($args[$i])){
  12. $args[$i] = $mysqli->real_escape_string($args[$i]);
  13. }else{
  14. return false;
  15. }
  16. }
  17. return $mysqli->query(vsprintf($query,$args));
  18. }
  19. function fetch_all($result,$type=MYSQLI_NUM){
  20. if(method_exists('mysqli_result', 'fetch_all')){
  21. $res = $result->fetch_all($type);
  22. }else{
  23. for($res = array(); $tmp = $result->fetch_array($type);){
  24. $res[] = $tmp;
  25. }
  26. }
  27. return $res;
  28. }
  29. ?>