블로그 이미지
hengki
우쭈쭈우쭈쭈

calendar

      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

'php'에 해당되는 글 3

  1. 2009/04/15 PHP mysql 접근 클래스
  2. 2009/04/01 PHP RSS 리더
  3. 2009/04/01 PHP XML 파서(1)
2009/04/15 13:22 Hengki's Program/PHP

  PHP+MySql 은 많은 사용자들에게 사랑 받고 있다. 가장 큰이유는 무료 라는 것과, 알려진 소스도 많다는 것도 있을수 있다.(무료라서 알려진소스가 많을수도) 또한 일반 사용자도 쉽게 서버를 구축할수 있는 이유도 있다. 홈페이지 관련 아르바이트를 하다보면 "Apmsetup" 이라는 프로그램에 "제로보드 4.x" 가 설치되어 운영되어 지는 것을 많이 볼수도 있다.(심지어 대량의 트레픽을 요구하는 곳에서도...) 그만큼 php와 mysql은 대중적으로 사용되어 지고있다.  

  머 여기 까진 주저리 였고 php에서 mysql을 접근하는 클래스를 구성해 보았다. php 하면서 항상 사용되는 소스이다보니 php로 뭘한다 싶으면, 먼저 이 파일부터 옮겨놓고 프로그래밍을 시작할 정도로 유용하게 사용하고있다. 

  따로 메뉴얼은 만들지 않겠다. php를 좀(?) 했다면 척보면 알정도의 소스이기 때문이다.(결코 귀찬아서 그런건 아니다) 대략 설명하자면 생성자 부분에서 db설정을 넣어주고 사용하면된다.


<?php

class dbControl{

 // -------------------------------------------------------------------
 // 생성자
 // -------------------------------------------------------------------
 function __construct(){

  // db서버 주소
  $hostname = "";
  // 사용자 id
  $username = "";
  // 사용자 패스워드
  $password = "";
  // 사용 db
  $dbname = "";

  $connect = mysql_connect($hostname, $username, $password) or die("SQL 서버에 접속할 수 없습니다.");
  $result = @mysql_select_db($dbname,$connect) or die("데이터베이스를 선택할 수 없습니다.");
 }

 // -------------------------------------------------------------------
 // 테이블 생성
 // -------------------------------------------------------------------
 function db_create($Tname,$Attribute){
  $sql="create table ".$Tname."(".$Attribute.");";
  $result = mysql_query($sql);
  if(!$result) echo "테이블생성 실패";
 }
                   
 // -------------------------------------------------------------------
 // 데이터 삽입
 // -------------------------------------------------------------------
 function db_insert($Tname, $data){
  $sql="insert into ".$Tname." VALUES (".$data.");";
  $result = mysql_query($sql);
  if(!$result) echo "데이터입력 실패";
 }

 // -------------------------------------------------------------------
 // 데이터 삭제
 // -------------------------------------------------------------------
 function db_delete($Tname, $where){
  $sql="delete from ".$Tname." WHERE ".$where.";";
  $result = mysql_query($sql);
  if(!$result) echo "데이터삭제 실패";
 }

 // -------------------------------------------------------------------
 // 데이터 수정
 // -------------------------------------------------------------------
 function db_modify($Tname, $set, $where){
  $sql="update ".$Tname." SET ".$set." WHERE ".$where;
  $result = mysql_query($sql);
  if(!$result) echo "데이터수정 실패";
 }


 // -------------------------------------------------------------------
 // 데이터 출력
 // -------------------------------------------------------------------
 function db_print($Tname){
  $sql="select * from ".$Tname.";";
  $result=mysql_query($sql);

  if($result){
   while($row = mysql_fetch_array($result)) $total[]=$row;
   return $total;
  }else echo "데이터출력 실패";
 }

 // -------------------------------------------------------------------
 // 쿼리 입력
 // -------------------------------------------------------------------
 function db_query($sql){
  $result = mysql_query($sql);
  if($result){
   $row = mysql_fetch_array($result);
   return $row;  
  }else{
   echo "쿼리 실패";
  }
 }
}

?>


※ 사용 예시

 <?
 include "dbControl.php";    // basic_db lib

 $db = new dbcontrol;     // db객체생성

 $row = $db->db_print("table_name");  // table_name의 데이터를 가져옴

 foreach($row as $data){
  echo $data[속성1];     // 속성1의 값을 출력
  echo $data[속성2];     // 속성2의 값을 출력
 }
?>


저작자 표시
posted by hengki
2009/04/01 13:50 Hengki's Program/PHP

PHP로 구현된 간단한 RSS리더 입니다.
lastRSS라는 사이트에서 GNU 라이센스로 배포하고 있습니다. (망했나보네요.. 사이트가 closed..)
사용법은 제가 나름 사용해봤본 소스입니다.

다운로드




1. 사용법
<?php
include "./lastRSS.php";
// -------------------------------------------------------------------
// 출력할 라인수를 결정합니다.
$line = 50;
// -------------------------------------------------------------------
// 설정 URL을 지정합니다.
$rssurl = "";
// -------------------------------------------------------------------
// rss설정
$rss = new lastRSS;
$rss->cache_dir = './cache/';   //케쉬를만들 폴더설정
$rss->cache_time = 0;     //케쉬생성주기
$rss->cp = 'UTF-8';      //인코딩
$rss->date_format = 'y-m-d';   //날짜형식 년월일
   
if ($rs = $rss->get($rssurl)) {
  $i=0;
  foreach($rs['items'] as $item) {
// -------------------------------------------------------------------
// 인코딩및 cdta 태그를 지우는 부분입니다.
   $item[link]=str_replace("<![CDATA[", "",$item[link]);
   $item[link]=str_replace("]]>", "",$item[link]);
   $item[link]=iconv('UTF-8', 'EUC-KR', $item[link]);
   $item[title]=str_replace("<![CDATA[", "",$item[title]);
   $item[title]=str_replace("]]>", "",$item[title]);
   $item[title]=iconv('UTF-8', 'EUC-KR', $item[title]);
   $item[description]=str_replace("<![CDATA[", "",$item[description]);
   $item[description]=str_replace("]]>", "",$item[description]);
   $item[description]=iconv('UTF-8', 'EUC-KR', $item[description]);
   $item[author]=iconv('UTF-8', 'EUC-KR', $item[author]);
   $item[category]=iconv('UTF-8', 'EUC-KR', $item[category]);
// -------------------------------------------------------------------
// 출력 HTML 부분
   ?>
    제목 :  <?=$item['title']?>   <br/>
    링크 :  <?=$item[link]?>   <br/>
    내용 :  <?=$item[description]?>  <br/>
    작성자 : <?=$item[author]?>   <br/>
    작성일 : <?=$item[pubDate]?>   <br/>
    <br/><br/>
   <?
// -------------------------------------------------------------------
// 라인수결정
   if($i++ > $line-2) break;
  } 
}
// -------------------------------------------------------------------
// RSS경로가 잘못되거나 열수 없을때
else {?>
경로를 찾을수 없거나, 서버를 찾을수 없습니다.
<?
}
?>
posted by hengki
2009/04/01 13:40 Hengki's Program/PHP
php5에서는 xml lib를 제공하지만 4.x는 제공하지 않는걸로 알고있습니다.
결국 xml파서를 만들어 사용해야는데요.
고맙게도criticaldevelopment.net 에서 GNU라이센스를 사용하여 배포하고있습니다.

참고문서 : http://www.criticaldevelopment.net/xml/doc.php
파일다운 : 



1. 속성
tagData : 태그값
tagAttrs : 태그 속성값
tagParents : This member contains the number of parents this object has before the document root. This number, currently, is only used to determine how many tabs are required to nicely format the XML output.
tagChildren : This member is an array of references to all of the direct child tags of the given object, in order of occurance in the XML document. It is simply an alternative to accessing the children tags by their names, and is used when names are arbitrary or unknown.
tagName : This member contains the name of the current tag. Again, it is only used internally for the proper output of the XML document.

2. 예제 xml

<?xml version='1.0' encoding='utf-8'?>
<Widget>
<WidgetPrefs>
<title>제목</title>
  <directory_title>디렉토리제목</directory_title>
 </WidgetPrefs>
 <Content src="index.html"></Content>
</Widget>

3.사용법

<?php
//기본적으로 들어가는 부분
include "parser_php4.php";                       // 클래스 파일 include
$xml = file_get_contents("./ex.xml");         // 파싱할 대상XML 가져오기
$parser = new XMLParser($xml);             // 객체생성 parser라는 객체를 생성함
$parser->Parse();                                  // Parse()메소를 호출하여 xml을 dom 방식으로 파싱함

//파싱된 xml결과값을 사용하는 방법
echo $parser->document->widgetprefs[0]->title[0]->tagData;
 // 타이틀 데이터를 가져올때 (하나의 데이터를 지정해서 가져올 경우)
// "위젯 제목을 제공하는~~" 출력됨
echo $parser->document->content[0]->tagAttrs['src'];
 // 속성값 가져오기
 // "index.html"이 출력됨 
echo $parser->GenerateXML();              
// 위 ex.xml과 똑같은 xml 문서가 출력됨
?>

posted by hengki
prev 1 next