> Hello World !!!

     

@syaku

PHP 디비 커넥션 : Database Connection

written by Seok Kyun. Choi. 최석균

 Connection.php


<?php

/**
 * @class Connection ver 1.1
 * @brief database Connection
 *
 * registered date 20100502
 * programmed by Seok Kyun. Choi. 최석균
 * http://syaku.tistory.com
 */

class Connection {
  var $is_new = false; // 새로운 디비 링크
  var $host = 'localhost';  // 호스트
  var $user = 'root'; // 계정
  var $pass = 'x'; // 암호
  var $source = 'x'; // 데이터베이스

  var $pagenum=10;
  var $pagecount=9;
  var $conn,$result;
  var $query = null;

  var $page = 1;
  var $page_row = 10;
  var $page_link = 10;
  var $total_count = 0;
  var $total_page = 0;
  var $start_page = 0;
  var $now_page = 0;
  var $end_page = 0;
  var $start_idx = 0;
  var $virtual_idx =0;

  // 에러처리
  var $error = false;
  var $message = "";
  
  function Connection() {
    $this->conn = $this->mysql();
  }

  function mysql($is_new = false) {
  
    if(!$this->conn || $is_new){
      $this->conn = @mysql_connect($this->host, $this->user, $this->pass) or $this->error();
      @mysql_select_db($this->source, $this->conn) or $this->error();
    }

    return $this->conn;
  }

  function close(){
    if($this->conn) {
      @mysql_close($this->conn) or $this->error();
    }
  }

  function error(){
    $this->error = true;
    $this->message = mysql_error() . "\n<br />" . $this->query . "\n<br />";
  }

  function result() {
    $r = @mysql_query($this->query,$this->conn);
    if (!$r) {
      $this->error();
    }

    return $r;
  }

  function fetch($obj,$mode = 'array') {
    switch($mode){
      case 'array':
        return mysql_fetch_array($obj);
      break;
      case 'object':
        return mysql_fetch_object($obj);
      break;
      case 'row':
        return mysql_fetch_row($obj);
      break;
      case 'rows':
        return mysql_affected_rows($obj);
      break;
      case 'num_rows':
        return mysql_num_rows($obj);
      break;
      case 'result':
        return mysql_free_result($obj);
      break;
    }

  }

  // 현재 페이지번호
  function setPage($page) {
    if($page == '' || $page == 0) {
      $this->page = 1;
    } else {
      $this->page = $page;
    }
  }

  // 페이지 인덱스
  function index($total_count) {
    // 총 레코드수
    $this->total_count = $total_count;
    // 총 페이지수
    $this->total_page = (($this->total_count - 1) / $this->page_row) + 1;
    // 시작 페이지 번호
    $this->start_page = (($this->page - 1) / $this->page_link) * $this->page_link + 1;
    // 현재 페이지 번호
    $this->now_page = ($this->start_page / $this->page_link) + 1;
    // 마지막 페이지 번호
    $this->end_page = $this->start_page + ($this->page_link - 1);
    // 시작 페이지 인덱스 번호
    $this->start_idx = ($this->page - 1) * $this->page_row;
    // 페이지 가상번호
    $this->virtual_idx = $this->total_count - ($this->page_row * ($this->page - 1));
  }
}
?>

 디비 연결 예제

<?php
  include_once "./common/classes/Connection.php";

  $conn = new Connection();

  $page = $_GET['page'];
  $conn->setPage($page);

  // page index
  $conn->query = "select count(*) from test";
  $rs = $conn->result();
  $total_count = $rs[0];

  $conn->index($total_count);

  // query string
  $conn->query = "select * from test limit " . $conn->start_idx . ",10"

  // mysql execute
  $result = $conn->result();

  // error
  if ($conn->error) { echo $conn->message; }

  $cnt = 0;
  while($rs = $conn->fetch($result)) {
    $num = $conn->virtual_idx - $cnt;
    $name = $rs['name'];
    echo $name;
  }

  $conn->close();
?>

Syaku Blog by Seok Kyun. Choi. 최석균.
posted syaku blog


http://syaku.tistory.com