2014-07-22 2 views
0

새로운 스크립트를 작성 중이며 현재 데이터베이스 클래스를 작성 중입니다. 사용자의 IP 주소를 확인하는 함수를 만들었습니다. 테스트를하러 갔을 때 나는 die('It Worked');이 아닌 빈 페이지가 남았습니다.내 기능에 문제가있을 수 있습니까?

나는 많은 양의 코드를 게시하는 것을 좋아하지 않지만, 지금까지 내가 수업을 게시했다면 더 이해할 수있을 것이다.

class.Database.inc

<?php 
     require_once('config.php'); // Configuration file 

    /** 
    * MySQLi database; only one connection is allowed. 
    */ 
    class Database { 
    // Database credentials from config file 
     private $_DATABASE_SERVER = DB_SERVER; 
     private $_DATABASE_USER = DB_USER; 
     private $_DATABASE_PASS = DB_PASS; 
     private $_DATABASE_NAME = DB_NAME; 

     private $_connection; 
     // Store the single instance. 
     private static $_instance; 

     /** 
     * Get an instance of the Database. 
     * @return Database 
     */ 
     public static function getInstance() { 
     if (!self::$_instance) { 
      self::$_instance = new self(); 
     } 
     return self::$_instance; 
     } 

     /** 
     * Constructor. 
     * Database connection (server, user, password, name) 
     */ 
     public function __construct() { 
     $this->_connection = new mysqli($this->_DATABASE_SERVER, $this->_DATABASE_USER, $this->_DATABASE_PASS, $this->_DATABASE_NAME); 
     // Error handling. 
     if (mysqli_connect_error()) { 
      trigger_error('Failed to connect to MySQL: ' . mysqli_connect_error(), E_USER_ERROR); 
     } 
     } 

     /** 
     * Empty clone magic method to prevent duplication. 
     */ 
     private function __clone() {} 

     /** 
     * Get the mysqli connection. 
     */ 
     public function getConnection() { 
     return $this->_connection; 
     } 

    /** 
    * DATABASE IP CHECK FUNCTION 
    */ 
     public function checkIp($user_ip) { 
      $db = self::getInstance(); 
      $mysqli = $db->getConnection(); 

      $sql_query = "SELECT ip FROM "; 
      $sql_query .= "ip_address WHERE "; 
      $sql_query .= "ip = '$user_ip'"; 

      $result = $mysqli->query($sql_query) or die(mysqli_error($mysqli)); 
      if ($row = $result->fetch_assoc()) { 
       die('It Worked!'); 
      } 
     } 
    } 
    ?> 

문제는 파일의 하단은 checkIp(); 함수 향해있는 기능이다.

나는이 내용으로 test.php 파일을 만들었습니다.

<?php 
require_once('class.Database.inc.php'); 

Database->checkIp('1'); 
?> 

내가 잘못 기능에 액세스하려고 시도했는지 또는 다른 것이 아닌지 확실하지 않습니다. 누군가가 궁금해하는 경우에 대비하여 값 1이 데이터베이스에 있습니다.

+0

당신의 오류 로그를 읽고이 문제를 쉽게 식별 할 수는 있어요 있도록

당신은 오류보고를 보장 할 수는 켜져 . –

+0

싱글 톤. 그러지 마, 그냥 ... 하지마. 추신 : 클래스 '죽을'및 드물게'trigger_error', 예외를 throw합니다. –

+0

@MartinBean error_log에 아무것도 없음 –

답변

1

싱글 톤을 피하고 필요한 경우 Database 개체를 만듭니다. Database->checkIp('1') 대신에 더 정적 인 함수를 호출하는 대신 Database의 인스턴스를 사용해야합니다. 또한 checkIp 함수는 이미 $this을 통해 getConnection 함수에 액세스해야합니다. 이미 클래스에 있으므로 새 인스턴스를 만들 필요가 없기 때문입니다. ini_set('display_errors', 1);

<?php 
    require_once('config.php'); // Configuration file 

/** 
* MySQLi database; only one connection is allowed. 
*/ 
class Database { 
// Database credentials from config file 
    private $_DATABASE_SERVER = DB_SERVER; 
    private $_DATABASE_USER = DB_USER; 
    private $_DATABASE_PASS = DB_PASS; 
    private $_DATABASE_NAME = DB_NAME; 

    private $_connection; 

    /** 
    * Constructor. 
    * Database connection (server, user, password, name) 
    */ 
    public function __construct() { 
    $this->_connection = new mysqli($this->_DATABASE_SERVER, $this->_DATABASE_USER, $this->_DATABASE_PASS, $this->_DATABASE_NAME); 
    // Error handling. 
    if (mysqli_connect_error()) { 
     trigger_error('Failed to connect to MySQL: ' . mysqli_connect_error(), E_USER_ERROR); 
    } 
    } 

    /** 
    * Get the mysqli connection. 
    */ 
    public function getConnection() { 
    return $this->_connection; 
    } 

/** 
* DATABASE IP CHECK FUNCTION 
*/ 
    public function checkIp($user_ip) { 
     $mysqli = $this->getConnection(); 

     $sql_query = "SELECT ip FROM "; 
     $sql_query .= "ip_address WHERE "; 
     $sql_query .= "ip = '$user_ip'"; 

     $result = $mysqli->query($sql_query) or die(mysqli_error($mysqli)); 
     if ($row = $result->fetch_assoc()) { 
      die('It Worked!'); 
     } 
    } 
} 
?> 

사용

error_reporting(E_ALL);

:

<?php 
    require_once('class.Database.inc.php'); 
    $database = new Database(); 
    $database->checkIp('1'); 
?> 
+0

감사합니다. –

관련 문제