· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
Linuxdoc Sgml/PHP-HOWTO

PHP ÇÏ¿ìÅõ

PHP ÇÏ¿ìÅõ

Al Dev (Alavoor Vasudevan) alavoor@yahoo.com

v8.0, 26 Sep 2000 À±±¤È£(KwangHo, Yun), airplanez@kldp.org PHP-HOWTO_kr.sgml, v0.01 2000/11/07 15:50
À̹®¼­´Â ¾î¶»°Ô PHP ÇÁ·Î±×·¥À» ¸¸µå´ÂÁö¿Í Windows 95 GUI ¾îÇø®ÄÉÀ̼ÇÀ» ÈǸ¢ÇÑ PHP + HTML + DHTML + XML + Java applets + Javascript ·Î ¹Ù²Ù´Â °ÍÀ» ¼³¸íÇÒ°ÍÀÌ´Ù. À̹®¼­¿¡ ÀÖ´Â Á¤º¸´Â PHP°¡ Æ÷Æà µÇ¾îÀÖ´Â Linux, Windows 95/NT, OS/2, Unix °è¿­ÀÇ Solaris, HPUX, AIX, SCO, Sinix, BSD, µîµîÀÇ ¿î¿µÃ¼°è¿¡ Àû¿ëµÈ´Ù.

1. ¼Ò°³

PHP ´Â 'Hypertext Pre-Processor'¸¦ ÀǹÌÇÏ°í ¼­¹ö»çÀ̵å(server side) HTML ½ºÅ©¸³ÆÃ/ÇÁ·Î±×·¡¹Ö ¾ð¾îÀÌ´Ù. PHP´Â µ¿ÀûÀÎ À¥ÆäÀÌÁö¸¦ ¸¸µé¼ö ÀÖ°ÔÇØÁØ´Ù. PHP°¡ »ç¿ëµÈ À¥ÆäÀÌÁö´Â ÀϹÝÀûÀÎ HTML ó·³ Ãë±ÞµÇ¸ç Æò¹üÇÑ HTML ÆäÀÌÁöó·³ »ý¼º°ú ¼öÁ¤À» ÇÒ¼ö ÀÖ´Ù.

¸¹Àº ȸ»çµéÀÌ PHP´Â "Á߿亸¾È°ú ¾ö°ÝÇÑ ºñ¹Ð"À» À¯ÁöÇÒ¼ö ÀÖ´Â ÄÄÇ»ÅÍ ¾ð¾î¶ó°í ¸»ÇÏÁö¸¸, ±×·¯³ª Áö±ÝÀº À߾˷ÁÁö°í ¸¹ÀÌ ¾²ÀÌ´Â À¥, ÀÎÅͳÝ, ÀüÀÚ»ó°Å·¡¿Í B2B(business-to-business) ÇÁ·ÎÁ§Æ®À» À§ÇÑ ½ºÅ©¸³Æ® ¾ð¾î°¡ µÇ¾ú´Ù. ±×·³¿¡µµ ºÒ±¸ÇÏ°í ¿äÁò ¸¹Àº °æÀïȸ»çµéÀÌ PHP¾ð¾î¸¦ º¸¾È¿¡ ´ëÇؼ­ ¾ÈÀüÇÏÁö¾Ê´Ù°í ÇÑ´Ù(°æÀïÀÚµé).(¿ªÁÖ:¹ø¿ªÀÌ ¿µ ÀÌ»óÇϳ×..)

PHP´Â Àü¼¼°è¿¡ µ¹Ç³À» ÀÏÀ¸Å³ °ÍÀ̸ç IT¾÷°è¸¦ ³î¶ø°Ô Á¡À¯ÇÏ°Ô µÉ°ÍÀÌ´Ù!! PHPÀÇ ÆÄ¿ö´Â Å©·Î½º-Ç÷§Æû ÀÌ°í ¾î´À°÷¿¡¼­µµ ½ÇÇàµÈ´Ù!! PHP´Â ¸®´ª½º, À©µµ 95/98/NT, À©µµ 2000, ¼Ö¶ó¸®½º, HPUX¿Í ¸ðµç À¯´Ð½º °è¿­¿¡¼­ ½ÇÇàµÈ´Ù. PHP´Â ÇѹøÀÛ¼º µÇ¸é ¾îµð¼­³ª ½ÇÇàµÈ´Ù. PHP´Â ¾ÆÆÄÄ¡, ¸¶ÀÌÅ©·Î¼ÒÇÁÆ® IIS µîµîÀÇ ¸¹Àº À¥¼­¹ö¿¡¼­ ½ÇÇàµÈ´Ù.

PHP´Â ÀÚ¹Ùº¸´Ù 5¿¡¼­ 20¹è±îÁö ºü¸£´Ù!! PHP´Â »ç¿ëÇϴµ¥ ¹«Ã´½±°í, ¸Å¿ì º¹ÀâÇÑ À¥/ÀüÀÚ»ó°Å·¡ ¾îÇø®ÄÉÀ̼ÇÀ» ¸Å¿ì ºü¸£°í ªÀº ½Ã°£¿¡ °³¹ßÇÒ¼ö ÀÖ´Ù.

PHP´Â °´Ã¼ÁöÇâÀûÀΠƯ¡°ú ÀÚ¹Ù, C++, PERL°ú "C"·Î ºÎÅÍ ÈǸ¢ÇÑ Æ¯Â¡À» °¡Á®¿Ô´Ù. PHP¾ð¾î´Â ÀÚ¹Ù, C++, PERL°ú CÀÇ ÈǸ¢ÇÑ Æ¯Â¡ÀÇ °áÇÕüÀÌ´Ù.

PHP´Â ¸ðµç ½ºÅ©¸³Æ®/ÇÁ·Î±×·¥ ¾ð¾îÀÇ ÁøÁ¤ÇÑ º¸¼®ÀÌ¸ç °ð Àü¼¼°è ÇÁ·Î±×·¡¸ÓÀÇ "¸ÞÄ«"°¡ µÉ°ÍÀÌ´Ù!! PHP´Â À©µµ 95/NT ¿Í ¸ðµç À¯´Ð½º¿¡¼­ ½ÇÇàµÇ±â ¶§¹®¿¡ °Å´ëÇÑ »ç¿ëÀÚÃþ°ú ¸¹Àº °³¹ßÀÚÃþÀ» °¡Áø´Ù.

PHP´Â º¸´Ù ºü¸£°Ô ½ÇÇàµÇµµ·Ï Á¨¿ÉƼ¸¶ÀÌÀú(Zend Optimizer)¸¦ »ç¿ëÇÏ¿© ÄÄÆÄÀÏ°ú ÃÖÀûÈ­(optimized)½Ãų¼ö ÀÖ´Ù. Á¨¿ÉƼ¸¶ÀÌÀú(Zend Optimizer)´Â PHP ¹öÀü 4.0¿¡¼­ ºÎÅÍ PHP¿¡ ÅëÇյǾú´Ù. - 2009.4.17 ¿©ÀüÈ÷ zend.com ¿¡¼­ ¹Þ¾Æ ¼³Ä¡ÇØ¾ß ÇÕ´Ï´Ù. php ÀÇ ¿£Áø À̸§ÀÌ zend engine ÀÌ¶ó¼­ Çò°¥¸± ¼ö´Â ÀÖÁö¸¸ zend optimizer ´Â ÅëÇÕµÇÁö ¾Ê¾Ò½À´Ï´Ù. ¼ÛÈ¿Áø

´ç½ÅÀº ÀüÀÚ»ó°Å·¡(e-commerce) ÇÁ·ÎÁ§Æ®¸¦ Çϴµ¥ º¸Åë PHP (70% ÄÚµå) + HTML/DHTML/XML (25% ÄÚµå) + Javascript (5% ÄÚµå Ŭ¶óÀ̾ðÆ® Ãø) ÀÇ Á¶ÇÕÀ» »ç¿ëÇÒ°ÍÀÌ´Ù.

2. PHP ³»·Á¹Þ±â

2.1 Microsoft Windows 95/98/NT/2000¿¡ PHP ¼³Ä¡

PHP´Â Microsoft Windows Ç÷¿Æû¿¡¼­ ¸Å¿ì ÀαâÀÖ°í ³î¶ø°Ôµµ MS ÀÇ ASP ½ºÆ®¸³Æ® ¾ð¾îº¸´Ù ´õ ÀαâÀÖ´Ù!! PHPÀÇ °¡ÀåÅ« ÀåÁ¡Àº MS À©µµ¿Í ¸®´ª½º, À¯´Ð½º¿¡¼­ °³¹ßÇÒ¼ö ÀÖ°í ¹Ý´ë·Î »ç¿ëÇÒ¼öµµ ÀÖ´Ù´Â °ÍÀÌ´Ù!!

MS À©µµ 98/NT/2000 ¿¡¼­ PHPÀÇ ¼ö¿ä°¡ ¸¹±â ¶§¹®¿¡ ½ÇÇà°¡´ÉÇÑ ¼³Ä¡ÇÁ·Î±×·¥ÀÌ ÁغñµÇ¾î ÀÖ´Ù. ´ÜÁö ½ÇÇàÈ­ÀÏÀ» ´õºíŬ¸¯ ÇÏ´Â °Í¸¸À¸·Î 2ºÐ¸¸¿¡ PHP¼³Ä¡°¡ ÀÚµ¿À¸·Î ÀÌ·ç¾îÁø´Ù. ½ÇÇà°¡´ÉÇÑ ¼³Ä¡È­ÀÏÀ» ´Ù¿î·ÎµåÇ϶ó.

2.2 À¯´Ð½ºµé°ú ´Ù¸¥ Ç÷¿Æû¿¡¼­ PHP ¼³Ä¡

PHP ¸ÞÀλçÀÌÆ® http://www.php.net ³ª ´Ù¿î·ÎµåÇÑ ÆÐÅ°ÁöÀÇ ¼³Ä¡ÆÄÀÏ ¿¡¼­ ¼³Ä¡°¡À̵å¿Í Áö½Ã¸¦ º¸¶ó.

3. PHP Æ©Å丮¾ó

¿©±â¿¡¼­´Â ´ç½ÅÀÇ ¼­¹ö°¡ PHP °¡ °¡´ÉÇÏ°í .php3·Î ³¡³ª´Â ¸ðµç È­ÀÏÀÌ PHP¿¡ ÀÇÇØ ½ÇÇàµÈ´Ù°í °¡Á¤ÇÑ´Ù.

ù¹ø° PHP°¡ °¡´ÉÇÑ ÆäÀÌÁö: ´ÙÀ½ÀÇ ¶óÀÎÀ» ³Ö¾î¼­ hello.php3¶õ È­ÀÏÀ» ¸¸µé¾î¶ó:


      <html>< head>< title >PHP Test< /title >< /head >
      < body>
      <?php echo "Hello World<P>"; ?>
      < /body>< /html>

CGI ½ºÅ©¸³Æ® ó·³ º¸ÀÌÁö ¾ÊÀ»°ÍÀÌ´Ù. Ưº°ÇÑ Å±׸¦ °¡Áö°í ÀÖ´Â º¸Åë HTML ó·³ »ý°¢Ç϶ó.

¸¸¾à ÀÌ°ÍÀ» ½ÇÇàÇߴµ¥ ¾Æ¹«·± °á°úµµ ³ªÅ¸³ªÁö ¾ÊÀ¸¸é, PHP°¡ ½ÇÇàµÇÁö ¾ÊÀº °ÍÀÌ´Ù. ´ç½ÅÀÇ °ü¸®ÀÚ¿¡°Ô ½ÇÇàµÇ°Ô ÇØ´Þ°í ¿äûÇ϶ó.

À̹ø ¿¬½À¿¡¼­´Â PHPÀÇ Æ¯º°ÇÑ Å±׸¦ º¸¿©ÁÖ±â À§ÇÑ°ÍÀÌ ¸ñÀûÀÌ´Ù. À̹ø ¿¬½À¿¡¼­ ¿ì¸®´Â PHP ű×ÀÇ ½ÃÀÛÀ» ¾Ë¸®´Â < ?php¸¦ »ç¿ëÇÏ¿´´Ù. ±×¸®°í PHP ¹®ÀåÀ» ³Ö°í ? > ű׸¦ ³¡¿¡ ³Ö¾ú´Ù. ´ç½ÅÀº ´ç½ÅÀÌ HTML È­ÀÏÀÇ ¿øÇÏ´Â °÷¿¡ ÀÌ·¸°Ô ÇÔÀ¸·Î¼­ PHP ¸ðµå¸¦ ½ÃÀÛÇÏ°í ³¡³¾¼ö ÀÖ´Ù.

¿ì¸®´Â ÆäÀÌÁö¸¦ º¸´Â »ç¶÷ÀÌ ¾î¶² ºê¶ó¿ìÀú¸¦ »ç¿ëÇÏ´ÂÁö °Ë»çÇغ¼°ÍÀÌ´Ù. ±×·¸°Ô ÇϱâÀ§ÇØ ¿ì¸®´Â ºê¶ó¿ìÀú°¡ º¸³»¿Â ¿äû¿¡ ´ëÇÑ ºÎºÐÀ» üũÇÑ´Ù. ÀÌ Á¤º¸´Â º¯¼ö¿¡ ÀúÀåµÈ´Ù. º¯¼ö´Â ¾ðÁ¦³ª ´Þ·¯»çÀÎ($)À¸·Î ½ÃÀÛÇÑ´Ù. ¿ì¸®°¡ °ü½ÉÀ» °¡Áö´Â º¯¼ö´Â $HTTP_USER_AGENT ÀÌ´Ù. ÀÌ°ÍÀ» º¸¿©ÁÖ±â À§Çؼ­ ¿ì¸®´Â ´ÙÀ½°ú °°ÀÌ ÇÒ¼ö ÀÖ´Ù:


      <?php echo $HTTP_USER_AGENT; ?>

Áö±Ý ´ç½ÅÀÌ ÀÌ ÆäÀÌÁö¸¦ º¸±â À§ÇØ »ç¿ëÇÏ´Â ºê¶ó¿ìÀú¿¡¼­´Â, ÀÌ·¸°Ô ³ª¿Â´Ù:

Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)

ÀÚ½ÅÀÇ À¥¼­¹ö¿¡ ÀÇÇØ ÀÚµ¿À¸·Î ¼³Á¤µÇ´Â ¸¹Àº ´Ù¸¥ º¯¼öµéÀÌ ÀÖ´Ù. ´ÙÀ½°ú °°Àº È­ÀÏÀ» ¸¸µêÀ¸·Î¼­ ¿ÏÀüÇÑ º¯¼öÀÇ ¸®½ºÆ®¸¦ ¾òÀ»¼ö ÀÖ´Ù:


      <?php phpinfo()?>

ºê¶ó¿ìÀú·Î ÀÌÈ­ÀÏÀ» ÀоîµéÀ̸é ÀÚ½ÅÇѵ¥ °¡¿ëÇÑ ¸ðµç º¯¼öÀÇ ¸®½ºÆ®¸¦ È­¸é°¡µæ ÇÑ Á¤º¸¿Í °°ÀÌ º¼¼ö ÀÖÀ»°ÍÀÌ´Ù.

PHP ű׾ȿ¡ ¿©·¯°³ÀÇ PHP ¹®ÀåÀ» Áý¾î³ÖÀ»¼ö ÀÖÀ¸¸ç ÇϳªÀÌ»óÀÇ echo ¹®À» °¡Áö´Â ÄÚµåºí·°À» ¸¸µé¼ö ÀÖ´Ù.


      <?php
      if(strstr($HTTP_USER_AGENT,"MSIE")) {
          echo "You are using Internet Explorer<br>";
      }
      ?>

¿©±â¿¡¼­´Â PHP ºí·°ÀÇ Áß°£ÀÏÁö¶ó°í PHP¸ðµå¸¦ ¹þ¾î³ª´Â ¹æ¹ýÀ» º¸¿©ÁÙ°ÍÀÌ´Ù:


        <?php
        if(strstr($HTTP_USER_AGENT,"MSIE")) 
        {
                ?>
                < center>< b>You are using Internet Explorer< /b>< /center>
                <?
        } 
        else 
        {
                ?>
                < center>< b>You are not using Internet Explorer< /b>< /center>
                <?
        }
        ?>

PHPÀÇ echo ¹®À» »ç¿ëÇÏ´Â ´ë½Å PHP ¸ðµå¸¦ ¹þ¾î³ª¼­ Á÷Á¢ HTML À» ³ÖÀ»¼ö ÀÖ´Ù. ¿©±â¼­ ¸»ÇÏ·Á°í ÇÏ´Â Áß¿äÇÏ°í °­·ÂÇÑ Á¡Àº ³í¸®ÀûÀÎ È帧ÀÌ ±úÁöÁö ¾Ê°í ³²¾Æ ÀÖ´Ù´Â °ÍÀÌ´Ù. ´ÜÁö HTML ºí·°µéÀº °á±¹ º¸´Â »ç¶÷¿¡°Ô¸¸ º¸³»Áö°Ô µÇ´Â°ÍÀÌ´Ù. ½ºÅ©¸³Æ®¸¦ ½ÇÇà½ÃÅ°¸é °á°ú´Â ´ÙÀ½°ú °°´Ù :

You are using Internet Explorer

Æû ´Ù·ç±â

PHPÀÇ °¡Àå À¯¿ëÇÑ ±â´ÉÁß Çϳª°¡ HTML Æû(form)À» ´Ù·ç´Â ¹æ¹ýÀÌ´Ù. ±×°ÍÀ» ÀÌÇØÇÏ´Â Áß¿äÇÑ ±âº»ÀûÀÎ °³³äÀº Æû¿¡ ³ª¿Â ¸ðµç ¿ä¼ÒµéÀÌ ÀÚµ¿ÀûÀ¸·Î Æû¿¡ ÀÖ´Â À̸§°ú °°Àº º¯¼ö·Î ÀÚµ¿ÀûÀ¸·Î ¹Ù²î¾î ºÒ·ÁÁø(target) °÷¿¡¼­ »ç¿ë µÈ´Ù´Â °ÍÀÌ´Ù. º¹ÀâÇÏ°Ô µé¸±°ÍÀÌ´Ù. ±×·¡¼­ ¿©±â °£´ÜÇÑ ¿¹Á¦°¡ ÀÖ´Ù. ´ÙÀ½°ú °°Àº ÆûÀ» °¡Áö´Â ÆäÀÌÁö°¡ ÀÖ´Ù°í °¡Á¤Ç϶ó :


      <form action="action.php3" method="POST">
      Your name: <input type=text name=name>
      You age: <input type=text name=age>
      <input type=submit>
      < /form>

¿©±â¿¡ ÀÖ´Â Æû¿¡´Â ¾î¶°ÇÑ Æ¯º°ÇÑ °Íµµ¾ø´Ù. ¾î¶² Ưº°ÇÑ Å±׵µ ¾ø´Â ´ÜÁö HTML Æû¿¡ ºÒ°ú ÇÏ´Ù. »ç¿ëÀÚ°¡ ÆûÀ» ÀÔ·ÂÇÏ°í Àü¼Û¹öÆ°À» ´©¸®¸é action.php3°¡ ºÒ·ÁÁø´Ù. ºÒ·ÁÁø È­ÀÏ¿¡´Â ´ÙÀ½°ú °°Àº °ÍÀÌ µé¾îÀÖÀ»°ÍÀÌ´Ù:
      Hi <?php echo $name?>.  You are <?php echo $age?> years old.

³î¶ø°Ôµµ $name°ú $age º¯¼ö´Â PHP¿¡ ÀÇÇØ ÀÚµ¿À¸·Î Á¤ÇØÁø´Ù!!

4. PHPÀ» À§ÇÑ IDE(ÅëÇÕȯ°æ) µµ±¸

¸¹Àº HTML ¿¡µðÅ͵éÀº PHP¸¦ Áö¿øÇÑ´Ù :

°¡±î¿î ½ÃÀÏ¿¡ ¸ðµç HTML ¿¡µðÅÍ¿Í XML ¿¡µðÅÍ°¡ "ºü¸¥ ¾îÇø®ÄÉÀÌ¼Ç °³¹ß" µµ±¸·Î PHP¸¦ Áö¿øÇÒ°ÍÀÌ´Ù. .

5. ³î¶ó¿î !!! PHP¸¦ À§ÇÑ ctags !

ÀÌ Å±׵éÀº »ó´çÈ÷ Áß¿äÇÏ°í vi, emacs, CRiSP, NEdit µî°ú °°Àº ¿¡µðÅÍ·Î ¼Ò½ºÄڵ带 Ž»ö(°Ë»ö)Çϴµ¥ ¾²¿©Áø´Ù. ¸¸¾à C, C++ ¶Ç´Â ÀÚ¹Ù·Î ÇÁ·Î±×·¥ÇÑ °ÍÀ» °¡Áö°í ÀÖ´Ù¸é ű׸¦ »ý¼ºÇϱâ À§ÇØ ctags ÇÁ·Î±×·¥À» »ç¿ëÇÒ¼ö ÀÖÀ»°ÍÀÌ´Ù. ¿Â¶óÀÎ ¸Þ´º¾ó ÆäÀÌÁö(online manual page)¸¦ º¸±âÀ§Çؼ­´Â ¸®´ª½º/À¯´Ð½º bash prompt ¿¡¼­ 'man ctags'¸¦ Ä¡¸é µÈ´Ù.

PHP¸¦ À§ÇÑ ptags ÇÁ·Î±×·¥Àº ´ÙÀ½¿¡ ÀÖ´Ù. ±×°ÍÀ» »ç¿ëÇϸé PHP ¼Ò½ºÄڵ带 À§ÇÑ Å±׸¦ »ý¼ºÇÒ¼ö ÀÖ´Ù. ptags¸¦ »ç¿ëÇÏ¸é ´ç½ÅÀÇ »ý»ê¼ºÀº 3¿¡¼­ 4¹è±î·® Çâ»óµÉ°ÍÀÌ´Ù.

¶ÇÇÑ http://metalab.unc.edu/LDP/HOWTO/Vim-HOWTO.html¿¡ ÀÖ´Â PHP, C, C++¸¦ À§ÇÑ Vim Ä÷¯ ÅؽºÆ® ¿¡µðÅ͸¦ ÂüÁ¶Ç϶ó.


// ptags.cpp·Î ÀÌÈ­ÀÏÀ» ÀúÀåÇÏ°í 
// g++ -o ptags ptags.cpp ·Î ÄÄÆÄÀÏÇ϶ó.
//*****************************************************************
// GNU/GPL ÀúÀÛ±ÇÀ» µû¸¥´Ù. Ãß°¡·Î º¹»ç¸¦ ÇϰԵǸé ÀúÀÚÀÇ À̸§,
// À̸ÞÀÏÀ» Æ÷ÇÔ½ÃÄѶó.
// ÀúÀÚ : Al Dev Email: alavoor@yahoo.com
// Usage : ptags *.php3 *.inc
//                 This will generate a file called tags
//*****************************************************************
#include <iostream.h>
#include <fstream>
#include <stdio.h> // for sprintf
#include <stdlib.h> // for system
#include <string.h> // for memset
#include <ctype.h> // for isspace

#define BUFF_LEN  1024
#define LOCATION  9

char *ltrim(char *dd);
char *rtrim(char *ee);

main(int argc, char **argv)
{
        if (argc < 2)
        {
                cerr << "\nUsage: " << argv[0] << " file .... " << endl;
                exit(0);
        }

        char fname[100] = "tag_file.out";
        FILE    *fpout;
        ofstream    fout(fname);
        if (fout.fail())
        {
                cerr << "\nError opening file : " << fname << endl;
                exit(-1);
        }
        //fpout = fopen(fname, "w");

        for (int ii = 1; ii < argc; ii++)
        {
                /*
                char buff[2024];

                sprintf(buff, "\\rm -f %s; ls %s > %s 2>/dev/null", outfile, argv[1], outfile);
                cout << "\nbuff = " << buff << endl;

                system(buff);
                fclose(fp);
                */
                FILE *fpin = NULL;
                fpin = fopen(argv[ii], "r");
                if (fpin == NULL)
                {
                        cerr << "\nError opening file : " << argv[ii] << endl;
                        exit(-1);
                }
                char buff[BUFF_LEN + 100];
                memset(buff, 0, BUFF_LEN +10);
                for ( ; fgets(buff, BUFF_LEN, fpin) != NULL; )
                {
                        char aa[BUFF_LEN + 100];
                        memset(aa, 0, BUFF_LEN +10);
                        strcpy(aa, buff);
                        ltrim(aa);

                        // Remove the trailing new line..
                        {
                                int tmpii = strlen(aa);
                                if (aa[tmpii-1] == '\n')
                                        aa[tmpii-1] = 0;
                        }
                        //cout << "aa is : " << aa << endl;
                        if (strncmp(aa, "function ", LOCATION) != 0)
                                continue;
                        //cout << buff << endl;

                        // Example tags file output is like -
                        // al2  al.c    /^al2()$/;"     f
                        {
                                char bb[BUFF_LEN + 100];
                                memset(bb, 0, BUFF_LEN +10);
                                strcpy(bb, & aa[LOCATION]);
                                char *cc = bb;
                                while (cc != NULL && *cc != '(')
                                        *cc++;
                                *cc = 0;
                                cc = rtrim(bb);
                                //cout << "bb is : " << bb << endl;
                                //cout << cc << "\t" << argv[ii] << "\t" << "/^" << aa << "$/;\"\tf" << endl;
                                fout << cc << "\t" << argv[ii] << "\t" << "/^" << aa << "$/;\"\tf" << endl;
                                //fprintf(fpout, "%s\t%s\t/^%s$/;\"f\n", cc, argv[ii], aa );
                        }

                        memset(buff, 0, BUFF_LEN +10);
                }
                fclose(fpin);
        }
        fout.flush();
        fout.close();
        //fclose(fpout);

        // Sort and generate the tag file
        {
                char tmpaa[1024];
                sprintf(tmpaa, "sort %s > tags; \\rm -f %s", fname, fname);
                system(tmpaa);
        }
}

char *ltrim(char *dd)
{
    if (dd == NULL)
        return NULL;

    while (isspace(*dd))
        dd++;
        
        return dd;
}

char *rtrim(char *ee)
{
    if (ee == NULL)
        return NULL;

        int tmpii = strlen(ee) - 1;
        for (; tmpii >= 0 ; tmpii--)
        {
                if (isspace(ee[tmpii]) )
                {
                        //cout << "\nis a space!!" << endl;
                        ee[tmpii] = 0;
                }
        }
        return ee;
}

6. PHP µð¹ö±ë

PHP ÇÁ·Î±×·¥À» µð¹ö±ë ÇÏ·Á¸é ´ÙÀ½ÀÇ ÇÔ¼ö¸¦ °¡Áö´Â "debug2.inc"À̶õ È­ÀÏÀ» ¸¸µé¾î¶ó :


<?php

/* ÀÌÁßÀ¸·Î ¼±¾ðµÇ´Â °ÍÀ» ¹æÁöÇϱâ À§ÇÑ º¯¼öÁ¤ÀÇ */
if (!defined("_DEBUG2_DEFINED_")) 
{
        define("_DEBUG2_DEFINED_", 1 );
}
else
        return; // ¸¸¾à ÀÌÈ­ÀÏÀÌ include µÆ´Ù¸é ¸®ÅÏ
# file name : debug2.inc
# PHP ¼Ò½º Äڵ带 µð¹ö±ëÇϱâ À§ÇÑ ÇÔ¼öµé
#*****************************************************************
# Copyright policy is GNU/GPL but additional request is
# that you include author's name and email on all copies
# Author : Al Dev Email: alavoor@yahoo.com
#*****************************************************************

# Usage of this functions -
# In your source code put something like -
# debug2_(__FILE__, __LINE__, "f_somevariable", $f_somevariable);
# And this will generate output in debug.out file.

//function debug2_($fname, $lname, $debug_var, $debug_value=0) {}

// Give read, exec for all on directory /debug2_logs
// chmod a+rwx /debug2_logs
// But here you need to open the file in append mode.
$fp_debug2 = fopen("/debug2_logs/debug.out", "a");
if ($fp_debug2 == false)
{
        print "<b>File open failed - global.var.inc<b>";
        exit;
}

function debug2_($fname, $lname, $debug_var, $debug_value=0)
{
        global $fp_debug2;

        //print "<br> debug_value is : $debug_value <br>";
        if (!$debug_value)
        {
                fwrite($fp_debug2, "\n ". $fname ."  ". $lname .": $debug_var");
        }
        else
        {
                fwrite($fp_debug2, "\n ". $fname . " ". $lname .": $debug_var = $debug_value");
        }
        //print "<br> f_cookie is : $f_cookie <br>";
}

// In your first page, which is generally index.php3 
// truncate the debug2_logs file in beginning of code
function init_debug_file()
{
        global $fp_debug2;

        $fp_debug2 = fopen("/debug2_logs/debug.out", "w");
        if ($fp_debug2 == false)
        {
                print "<b>File open failed - global.var.inc<b>";
                exit;
        }
        system("chmod a+rwx /debug2_logs/debug.out");
}

?>

º¸Åë index.php3·Î µÇ´Â ½ÃÀÛÆäÀÌÁöÀÇ PHP ¼Ò½ºÄڵ忡 ´ÙÀ½À» Áý¾î³Ö¾î¶ó.


<?php
        include ("debug2.inc");

        init_debug_file();
        // ´Ù¸¥ ¸í·ÉÀº ¿©±â¿¡.....
        // ...........
?>

µð¹ö±ë °ªÀ» ¾ò±âÀ§ÇØ PHP ¼Ò½ºÄÚµå ÆÄÀÏ¿¡ debug2_() ¶õ È£ÃâÀ» ¾Æ·¡¿Í °°ÀÌ ³Ö¾î¶ó


<?php
include ("debug2.inc");
debug2_(__FILE__, __LINE__, "f_somevariable", $f_somevariable);

function aa()
{
        $aa = 8;
        debug2_(__FILE__, __LINE__, "aa", $aa);
}
?>

PHP ÇÁ·Î±×·¥À» ½ÇÇàÇÏ°Ô µÇ¸é, °á°ú°¡ debug.out À̶õ È­ÀÏ¿¡ ÆÄÀÏÀ̸§, ¶óÀιøÈ£, º¯¼ö¸í °ú º¯¼ö°ªÀÌ ³ªÅ¸³­´Ù.

ÀϹøÀûÀ¸·Î debug2_()¸¦ »ç¿ëÇ϶ó. ÇÁ·Î±×·¥¿¡¼­ debug2_()ÀÇ È£ÃâÀº ÃÖÁ¾°á°ú Äڵ忡 ¾î¶°ÇÑ ¿µÇâµµ ÁÖÁö¾ÊÀ¸¸ç ½ÇÇà¿¡µµ ¾Æ¹«·± ¿µÇâÀ» ÁÖÁö¾Ê´Â´Ù. ¿Ö³ÄÇÏ¸é ¾Æ·¡¿¡ ±â¼úÇÑ°Í °°ÀÌ ÇÊÅ͸µ µÇ±â ¶§¹®ÀÌ´Ù. debug2_()¸¦ ŸÀÌÇÎÇÏ´Â ½Ã°£À» ÁÙÀ̱â À§ÇØ º¹»ç(copy)¿Í ºÙ¿©³Ö±â(paste)¸¦ »ç¿ëÇÒ¼ö ÀÖ´Ù. ¶ÇÇÑ Vi ¿¡µðÅÍÀÇ 'yank to buffer' ±â´ÉÀ» ÀÌ¿ëÇؼ­ º¹»çÇ϶ó.

°³¹ßÀÌ ¿Ï·áµÇ¸é Å×½ºÆ®ÇÏ°í ¼­¹ö¿¡ ¿Ã¸° Áغñ°¡ µÆÀ¸¸é ¼Ò½ºÄڵ忡¼­ debug2_ È£ÃâÀ» ÇÊÅ͸µ Ç϶ó. À¯´Ð½º ÇÁ·ÒÇÁÆ®¿¡¼­ -


bash$ mkdir production
bash$ grep -v debug2_  filea.php3 > production/filea.php3

¿©·¯°³ÀÇ È­ÀÏÀÌ ÀÖÀ¸¸é -
bash$ mkdir production
bash$ ls *.php3 | while read ans 
do 
        grep -v  debug2_ $ans > production/$ans
done

±×¸®°í ÀÌÁ¦ production ¿¡¼­ °³¹ßÇϴ°÷(¿µ¿ª)À» º¹»çÇ϶ó.

7. PHPÀÇ Á¦ÇÑ

¸ðµç »ç¶÷ÀÌ ÇÑ°è¿Í ´ÜÁ¡ÀÌ ÀÖµíÀÌ PHPµµ ¿¹¿Ü°¡ ¾Æ´Ï´Ù. ¾Æ·¡¿¡ Àִ°ÍÀº PHPÀÇ Á¦¾à(ÇÑ°è)ÀÌ´Ù.(±×·¡¼­ °æ°íÇÑ´Ù !!)

  1. PHP´Â 100% ¼ø¼öÇÑ °´Ã¼ÁöÇâ ½ºÅ©¸³Æ® ¾ð¾î°¡ ¾Æ´Ï´Ù. PHP´Â ÄÚµåÀÇ Å©±â°¡ 300,000 ¶óÀÎÀ» ³ÑÁö ¾ÊÀ¸¸é ±¦Âù´Ù. 300,000 ¶óÀÎÀÌ ³Ñ´Â PHPÄڵ带 °ü¸®Çϱâ´Â Á»´õ Èûµé¾îÁú°ÍÀÌ´Ù.
  2. PHP´Â "C"³ª "C++" ¾ð¾îÀÇ ¼º´ÉÀ» ÁÖÁö ¸ðÇÑ´Ù. ¿Ö³ÄÇϸé PHP´Â ½ºÅ©¸³Æ® ¾ð¾îÀÌ°í ½ÇÇà¹æ½ÄÀÌ ÀÎÅÍÇÁ¸®ÅÍ Çü½ÄÀ̱⠶§¹®¿¡ ÃÖÀûÈ­µÈ "C++"º¸´Ù ´Ù¼Ò ´À¸®´Ù. ÃÖ°íÀÇ ¼º´ÉÀ» ¿øÇÑ´Ù¸é "C++"°ú Ä¿³Ø¼Ç Ç®¸µ(connection pooling)µÈ µ¥ÀÌŸº£À̽º/¿û¼­¹ö¿Í ¿¬µ¿µÈ ºü¸¥ CGI ¸¦ »ç¿ëÇÏ°í C++ ÄÄÆÄÀÏ·¯ ¿ÉƼ¸¶ÀÌÁ®¸¦ "-03" ¿É¼ÇÀ» »ç¿ëÇ϶ó. PHP 4¿¡ ÀÖ´ÂÁ¨µå¿ÉƼ¸¶ÀÌÁ®´Â PHPÀÇ ¼º´ÉÀ» ¾î´ÀÁ¤µµ Çâ»ó½Ãų°ÍÀÌ´Ù.

ÇÑÆíÀ¸·Î´Â PHP´Â ¸¹Àº ÀåÁ¡À» °¡Áö°í ±× ÀåÁ¡Àº Á¦ÇÑÀ» º¸ÃæÇÒ¼ö ÀÖ´Ù -

  1. PHP ½ºÅ©¸³Æ® ¾ð¾î¿¡¼­´Â ÄÄÆÄÀÏ°ú ¸µÅ©°¡ »ý·«µÇ±â ¶§¹®¿¡ ¸Å¿ì ºü¸£°Ô À¥¾îÇø®ÄÉÀ̼ÇÀ» °³¹ßÇÒ¼öÀÖ´Ù.
  2. PHP ¿¡Çø®ÄÉÀ̼ÇÀº ¸Å¿ì ¾ÈÁ¤ÀûÀÌ°í ÀÚ¹Ù½ºÅ©¸³Æ®°¡ ºê¶ó¿ìÁ®¿¡ ÀÇÁ¸ÀûÀΰͰú ´Ù¸£°Ô ºê¶ó¾ÆÁ®ÀÇ ±â¼ú¿¡ ÀÇÁ¸ÀûÀÌÁö ¾Ê´Ù. PHP´Â ¾î¶² ¼­¹ö Ç÷¿Æû°ú ¾î¶² ºê¶ó¿ìÁ®¸¦ ¼±ÅÃÇؾßÇÏ´Â °Í¿¡ ´ëÇÑ ÀÚÀ¯¸¦ ÁÖ°í ºê¶ó¿ìÁ®´Â HTML ÀÌ PHP°¡ ¸¸µé¾î³Â´Ù´Â °ÍÀ» ¸ð¸¥´Ù!
  3. PHP´Â ¸ðµç SQL Å×ÀÌÅͺ£À̽º ¼­¹ö¿ÍÀÇ ¿¬°á¼ºÀÌ ¶Ù¾î³ª´Ù.
  4. PHP´Â ºÎºÐÀûÀ¸·Î °´Ã¼ÁöÇâ Ư¼ºÀ» °¡Áö°í ÀÖ´Ù.
  5. PHP´Â "C++", Perl, Javascript¿Í ºñ½ÁÇÑ ¹®¹ýÀ» °¡Áö°í ¼Ò½ºÄڵ带 Ž»öÇÒ¼ö ÀÖ´Â ÇÁ·Î±×·¥°ú ºñ½ÁÇÑ 'ptags/ctags'¸¦ °¡Áö°í ÀÖ´Ù.
  6. PHP´Â ¼Óµµ¸¦ Çâ»ó½Ãų¼ö ÀÖ´Â Á¨µå¿ÉƼ¸¶ÀÌÁ®¸¦ °¡Áö°í ÀÖ´Ù.
  7. PHP´Â ¸ðµç À¯´Ð½ºµé, ¸®´ª½º, À©µµ 95/NT/200 ¿¡¼­ ½ÇÇàµÇ¸ç ASP, JSP¿Í ´Ù¸¥°Íº¸´Ù ¼º´ÉÀÌ ÁÁ´Ù.
  8. PHP´Â ¸¹Àº »ç¿ëÀÚÃþ¿Í °³¹ßÀÚÃþÀ» °¡Áö°í ÀÖ´Ù.

°æ°í: ¸¸¾à 100% ¼ø¼öÇÑ °´Ã¼ÁöÇâ ½ºÅ©¸³Æ® ¾ð¾î¸¦ ¿øÇÑ´Ù¸é "¹Ýµå½Ã" PythonÀ» ¿°µÎÇضó. 'Python'Àº óÀ½ºÎÅÍ °´Ã¼ÁöÇâ ½ºÅ©¸³Æ®¾ð¾îÀÌ´Ù. 'Zope'¶ó°í ºÒ¸®´Â Python À¥ ¾îÇø®ÄÉÀÌ¼Ç ¼­¹ö¸¦ »ç¿ëÇÒ¼ö ÀÖ´Ù. 'Zope'´Â http://www.zope.org ¿¡¼­ ÀÖ°í pythonÀº http://www.python.org¿¡ ÀÖ´Ù.

8. °ü°èÀÖ´Â URLµé

C, C++¿¡ °ü°èÀÖ´Â ´ÙÀ½ Àå¼Ò¸¦ ¹æ¹®Ç϶ó -

9. À̹®¼­ÀÇ ´Ù¸¥ Æ÷¸ä

À̹®¼­´Â DVI, Postscript, Latex, Adobe Acrobat PDF, LyX, GNU-info, HTML, RTF(Rich Text Format), Plain-text, Unix man pages ¿Í SGML 11°¡ÁöÀÇ ´Ù¸¥ Æ÷¸äÀÌ ÀÖ´Ù.

À̹®¼­´Â http://www.sgmltools.org¿¡¼­ ±¸ÇÒ¼ö ÀÖ´Â "SGML-Tools"¶õ ÅøÀ» »ç¿ëÇؼ­ ÀÛ¼ºµÇ¾ú´Ù. ´ÙÀ½°ú °°Àº ¸í·ÉÀ» »ç¿ëÇؼ­ ¼Ò½º¸¦ ÄÄÆÄÀÏ ÇÒ¼ö ÀÖ´Ù.
  • sgml2html CVS-HOWTO.sgml (html È­ÀÏÀ» ¸¸µé±â À§ÇØ)
  • sgml2rtf CVS-HOWTO.sgml (RTF È­ÀÏÀ» ¸¸µé±â À§ÇØ)
  • sgml2latex CVS-HOWTO.sgml (latex È­ÀÏÀ» ¸¸µé±â À§ÇØ)

LaTex ¹®¼­´Â sgml2latex (¿Í dvips) ±×¸®°í ¾ÆÅ©·Î¹ÙÆ®(Acrobat) distill ( http://www.adobe.com) À» »ç¿ëÇÑ Æ÷½ºÆ®½ºÅ©¸³Æ® °á°ú 󸮷ΠPDFÈ­ÀÏ·Î ½±°Ô ¹Ù²Ü¼ö ÀÖ´Ù. ´ÙÀ½À½ ¸í·ÉµéÀÌ´Ù:


bash$ man sgml2latex
bash$ sgml2latex filename.sgml
bash$ man dvips
bash$ dvips -o filename.ps filename.dvi
bash$ distill filename.ps
bash$ man ghostscript
bash$ man ps2pdf
bash$ ps2pdf input.ps output.pdf
bash$ acroread output.pdf &

¶Ç´Â °í½ºÆ®½ºÅ©¸³(Ghostscript) ¸í·É ps2pdfÀ» »ç¿ëÇÒ¼ö ÀÖ´Ù. ps2pdf´Â Adobe's Acrobat Distiller product ÀÇ °ÅÀÇ ¸ðµç ±â´É°ú ºñ½ÁÇÏ°Ô µ¿ÀÛÇÑ´Ù. Æ÷½ºÆ®½ºÅ©¸³Æ® È­ÀÏÀ» PDF(Portable Document Format)À¸·Î ¹Ù²Ù¾îÁØ´Ù. ps2pdf´Â ¸í·É ½ºÅ©¸³Æ®(command script/¹èÄ¡ÆÄÀÏ)°¡ Ghostscript¸¦ ºÒ·¯³»°í , pdfwrite¶ó°í ºÒ¸®´Â Ưº°ÇÑ Ãâ·Â µð¹ÙÀ̽º¸¦ ¼±ÅÃÇÏ¿© ½ÇÇàµÈ´Ù. ps2pdf¸¦ »ç¿ëÇϱâ À§ÇØ Ghostscript¸¦ ÄÄÆÄÀÏ ÇÒ¶§ pdfwrite ÀåÄ¡°¡ makefile¿¡ Æ÷ÇԵǾî¾ßÇÑ´Ù; ÀÚ¼¼ÇÑ °ÍÀº Ghostscript ¼³Ä¡ ¹®¼­¸¦ º¸¾Æ¶ó.

À̹®¼­´Â ´ÙÀ½¿¡¼­ ãÀ»¼ö ÀÖ´Ù -

¶ÇÇÑ ´ÙÀ½ÀÇ ¹Ì·¯ »çÀÌÆ®¿¡¼­ À̹®¼­¸¦ ãÀ»¼ö ÀÖ´Ù -

±× dvi Æ÷¸äÀ¸·Î µÇ¾îÀÖ´Â ±× ¹®¼­¸¦ º¸±âÀ§Çؼ­´Â xdvi¶õ ÇÁ·Î±×·¥À» »ç¿ëÇ϶ó. xdvi ÇÁ·Î±×·¥Àº ·¡µåÇÞ ¸®´ª½ºÀÇ tetex-xdvi*.rpm ÆÐÅ°Áö¿¡ ÀÖ°í ¸Þ´º¹öÆ° ControlPanel | Applications | Publishing | TeX ¿¡ ÀÖ´Ù. dvi ¹®¼­¸¦ Àбâ À§ÇØ ´ÙÀ½ÀÇ ¸í·ÉÀ» ½ÇÇàÇ϶ó -

        xdvi -geometry 80x90 howto.dvi
        man xdvi
±×¸®°í ¸¶¿ì½º¸¦ »ç¿ëÇÏ¿© À©µµ¿ì Å©±â¸¦ Á¶ÀýÇ϶ó. È­»ìǥŰ, Page Up, Page Down keys ¿Í 'f', 'd', 'u', 'c', 'l', 'r', 'p', 'n' ¸¦ ÀÌ¿ëÇÏ¿© À§, ¾Æ·¡, Áß°£, ´ÙÀ½ÆäÀÌÁö, Àü ÆäÀÌÁö µîÀ» Ž»öÇÒ¼ö ÀÖ´Ù. ½ÇÇàÀ» ³¡³»·Á¸é 'x'¸¦ ´­·¯¶ó.

'gv'(ghostview)³ª 'ghostscript'¶õ ÇÁ·Î±×·¥À» »ç¿ëÇؼ­ Æ÷½ºÆ®½ºÅ©¸³Æ® È­ÀÏÀ» ÀÐÀ»¼ö ÀÖ´Ù. ·¡µåÇÞ ¸®´ª½º¿¡¼­ ghostscript ´Â ghostscript*.rpm ÆÐÅ°Áö¿¡ gv´Â gv*.rpm ÆÐÅ°Áö¿¡ ÀÖ´Ù. ±×¸®°í ControlPanel | Applications | Graphics ¸Þ´º ¹öÆ°¿¡ À§Ä¡ÇÑ´Ù. gv °¡ ghostscriptº¸´Ù ´õ Ä£±ÙÇÑ ÇÁ·Î±×·¥ÀÌ´Ù. ±×¸®°í ghostscript¿Í gv´Â OS/2, Windows 95 ¿Í NT ¿ëµµ Á¸ÀçÇÑ´Ù. ±×¸®°í ±× ¿î¿ëüÁ¦¿¡¼­ ÀÌ ¹®¼­µµ º¼¼ö ÀÖ´Ù.

Æ÷½ºÆ®½ºÅ©¸³Æ® ¹®¼­¸¦ Àбâ À§ÇØ ´ÙÀ½À» ÀÔ·ÂÇ϶ó -

                gv howto.ps
                ghostscript howto.ps

³×½ºÄÉÀÌÇÁ ³×ºñ°ÔÀÌÅÍ, ¸¶ÀÌÅ©·Î¼ÒÇÁÆ® ÀÎÅÍ³Ý ÀͽºÆú·Î·¯, ·¡µåÇÞ ¹Ù·Ð À¥ºê¶ó¿ìÁ® ¶Ç´Â 10°¡Áö ÀÌ»óÀÇ À¥ºÎ¶ó¿ìÁ®¿¡¼­ HTML Æ÷¸Ë¹®¼­¸¦ ÀÐÀ»¼ö ÀÖ´Ù.

LyX ¶ó´Â X-Windows¿ë ÇÁ·Î±×·¥À¸·Î latex ¹®¼­¸¦ ÀÐÀ»¼ö ÀÖ´Ù.

10. ÀúÀÛ±Ç

ÀúÀÛ±ÇÁ¤Ã¥Àº LDP(¸®´ª½º ¹®¼­ ÇÁ·ÎÁ§Æ®)ÀÇ GNU/GPLÀ» µû¸¥´Ù. LDP´Â GNU/GPL ÇÁ·ÎÁ§Æ®ÀÌ´Ù. Ãß°¡ÀûÀÎ ¿ä±¸ - ÀúÀÛÀÚÀÇ À̸§, À̸ÞÀÏÁÖ¼Ò¿Í ÀÌ ÀúÀ۱ǻçÇ×À» À¯ÁöÇ϶ó. ¸¸¾à ´ç½ÅÀÌ À̹®¼­¿¡ ¾î¶°ÇÑ ¼öÁ¤À̳ª ÷°¡¸¦ ÇÒ°æ¿ì À̹®¼­ÀÇ ÀúÀÚ¿¡°Ô ¾Ë·ÁÁÖ¾î¶ó.

11. ºÎ·Ï A Database Wrapper ¿¹Á¦

Á¦ÃâÀÚ : Barton Greg greg@createtech.com ÀÌÈ­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÁ®¿¡¼­ '´Ù¸¥À̸§À¸·Î ÀúÀå'À» ´©¸£°í pgsql.lib ¶ó°í ÀÔ·ÂÇϽÿÀ.


ÀÌ°ÍÀº PostgresSQLÀ» À§ÇÑ µ¥ÀÌÅͺ£À̽º ·¦ÆÛÀÌÁö¸¸ ´Ù¸¥ Çü½ÄÀÇ
µ¥ÀÌÅͺ£À̽º¸¦ À§ÇØ Á¶±Ý¸¸ °íÄ¡¸é »ç¿ëÇÒ¼ö ÀÖ´Ù.


<?php

if ($dbObjDefined  !=  1)
{
        $dbObjDefined  =  1;

        // Wrapper  class  for  database  calls
        class  dbObj
        {
                // Connection  handle  to  database
                var  $conn;

                // Default  connection  parameters
                var  $host  =  "YourSite.com";
                var  $user  =  "johndoe";
                var  $password  =  "pwd";
                var  $port  =  "5432";
                var  $dbname  =  "MyDB";

                // Open  initial  connection.  $params  is  
                // an  associative  array  holding
                // parameters  to  the  pg_Connect  function.
                function  init($params)
                {
                        if(isset($parame[host]))
                                $host  =  $parame[host];
                        else
                                $host  =  $this->host;

                        if(isset($parame[user]))
                                $user  =  $parame[user];
                        else
                                $user  =  $this->user;

                        if(isset($parame[password]))
                                $password  =  $parame[password];
                        else
                                $password  =  $this->password;

                        if(isset($parame[port]))
                                $port  =  $parame[port];
                        else
                                $port  =  $this->port;

                        if(isset($parame[dbname]))
                                $dbname  =  $parame[dbname];
                        else
                                $dbname  =  $this->dbname;

                        $this->conn  =  pg_Connect ( "  host=$host  user=$user  password=$password  port=$port  dbname=$dbname ");
                }

                // Send  SQL  to  database  connection.
                // Return  recordset  object  on  success.
                // Return  0  on  failure.
                function  exec($SQL)
                {
                        $this->resultset  =  pg_Exec($this->conn,  $SQL);

                        if  ($this->resultset)  
                        {
                                $recset  =  new  recordset;
                                $recset->init($this->resultset);
                                return  $recset;
                        }
                        else  
                        {
                                return  0;
                        }
                }

                function valid()
                {
                        return $this->resultset;
                }

                // Close  connection  to  database
                function  free()
                {
                        pg_close($this->conn);
                }
        };

        /*
        ** This  is  a  simple  recordset  class  which  can  be
        ** traversed  using  next(),  prev(),  and  current()  methods.
        ** It  is  initialized  from  a  resultset  returned  from  the
        ** function  "pg_Exec"  or  can  be  generated  by  a  call  to  the
        ** exec  method  from  the  dbObj class given above.
        ** Below "Tuples" means rows.
        */
        class  recordset
        {
                var  $resultset;
                var  $index;
                var  $numFields;
                var  $numTuples;

                function  init($newResultset)
                {
                        $this->resultset  =  $newResultset;
                        $this->index  =  0;
                        $this->numFields  =  pg_NumFields($this->resultset);
                        $this->numTuples  =  pg_NumRows($this->resultset);
                }

                // Used in display() below
                function valid()
                {
                        return $this->resultset;
                }

                // Get  a  value  by  row  number  and  either  
                // column  name  or  column  number
                function  getVal($row,  $col)
                {
                        return  pg_Result($this->resultset,  $row,  $col);
                }

                // Return  an  array  of  field  names
                function  getFields()
                {
                        for ($i=0;  $i < $this->numFields;  $i++)
                                $retArray[] = pg_FieldName($this->resultset,  $i);
                        return  $retArray;
                }

                // Get  number  of  columns  in  resultset
                function  getNumFields()
                {
                        return  $this->numFields;
                }

                // Get  a  tuple  (associative  array  of  
                // column  values)  by  row  number
                function  getTupleDirect($row)
                {
                        for ($i=0;  $i < $this->numFields;  $i++)
                        {
                                $retArray[pg_FieldName($this->resultset,  $i)]  = 
                                        pg_Result($this->resultset,  $row,  $i);
                        }
                        return  $retArray;
                }

                // Get  an  array  filled  with  all  values  in  a  column
                // (using  either  column  name  or  column  number)
                function  getColumn($col)
                {
                        for ($i=0; $i < $this->numTuples; $i++)
                                $retArray[]  =  pg_Result($this->resultset,  $i,  $col);
                        return  $retArray;
                }

                // Return  the  number  of  records  in  the  recordset
                function  getNumTuples()
                {
                        return  $this->numTuples;
                }

                // Get  tuple  pointed  to  by  the  current  index
                function  getTuple()
                {
                        if ($this->index >= 0  &&  $this->index  <  $this->numTuples)
                                return  $this->getTupleDirect($this->index);
                        else
                                return  0;
                }

                function valueof($col)
                {
                        if ($col < $this->numFields)
                        {
                                return pg_Result($this->resultset,  $this->index,  $col);
                        }
                        else
                        {
                                return "";
                        }
                }

                // Reached last row - end of rows ? Used in display() below
                function eof()
                {
                        return $this->index == $this->numTuples;
                }

                // Return  1  if  index  is  within  bounds  of  the  recordset
                function  current()
                {
                        if ($this->index >= 0  &&  $this->index  <  $this->numTuples)
                                return  1;
                        else
                                return  0;
                }

                // Increment  index. Used in display() below
                function  next()
                {
                        if ($this->index < $this->numTuples)
                        {
                                $this->index++;
                                return  1;
                        }
                        else
                        {
                                return  0;
                        }
                }

                // Decrement  index
                function  prev()
                {
                        if ($this->index  >=  0)
                        {
                                $this->index--;
                                return  1;
                        }
                        else
                        {
                                return  0;
                        }
                }

                // Reset  index  to  0 - See also first()
                function  reset()
                {
                        $this->index  =  0;
                }

                // See also reset(). Used in display() below
                function  first()  
                {
                        $this->index  =  0;
                }

                function last()
                {
                        $this->index = $this->numTuples -1 ;
                }

                // Used in display() below
                function showheader($col, $fmt = "")
                {
                        printf("\t< th %s>%s< /th >\n", $fmt, 
                        is_string($col) ? $col : pg_fieldname($this->resultset, $col));
                }

                // Used in display() below
                function showvalue($col, $fmt =  "", $def =  " ")
                {
                        $v = $this->valueof($col);
                        printf( "\t< td %s>%s< /td>\n", $fmt, $v ==  "" ? $def : $v);
                }

                function showurl($col, $fmt =  "")
                {
                        $v = $this->valueof($col);
                        if ( $v !=  "" ) 
                        {
                                printf("\t< td %s> < /td>\n", $fmt);
                        }
                        else 
                        {
                                printf( "\t< td %s>< a href=%s>%s< /a>< /td>\n", $fmt, $v, $v);
                        }
                }

                function display()
                {
                        if (!$this->valid() ) 
                        {
                                return;
                        }

                        printf( "<table cellspacing=1 cellpadding=1 border=1>\n");

                        printf( "<tr>\n");
                        for ($c = 0; $c < $this->cols; $c++ )
                        {
                                $this->showheader($c);
                        }
                        printf( "< /tr>\n");

                        $this->first();
                        while (!$this->eof())
                        {
                                printf( "<tr>\n");

                                for ($c = 0; $c < $this->cols; $c++)
                                {
                                        $this->showvalue($c);
                                }

                                printf( "< /tr>\n");
                                $this->next();
                        }
                        printf("< /table\n");
                }

                // Free  memory  allocated  to  recordset.
                function  free()
                {
                        pg_Freeresult($this->resultset);
                }
        };
}

?>

12. ºÎ·Ï B SQL abstraction Example

Á¦ÃâÀÚ : Gianugo Rabellino nemorino@opera.it ÀÌÈ­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÁ®¿¡¼­ '´Ù¸¥À̸§À¸·Î ÀúÀå'À» ´©¸£°í sqlabst.lib ¶ó°í ÀÔ·ÂÇϽÿÀ.



PX: PHP Code Exchange
<?php

/*
*    SAL - SQL Abstraction Library
*           version 0.01
*/

/*
** Set the variable $dbtype to any of the following
** values: MySQL, mSQL, Postgres, ODBC before including this library
*/
// $dbtype = "MySQL"; 
// $dbtype = "mSQL"; 
// $dbtype = "PostgreSQL"; 
// $dbtype = "ODBC"; 

// SQL_connect($host, $user, $password, $db)
// returns the connection ID

function SQL_connect($host, $user, $password, $db)
{
        global $dbtype;

        switch ($dbtype) 
        {
                case  "MySQL":
                        $conn=mysql_pconnect($host, $user, $password);
                        mysql_select_db($db);
                        return $conn;
                        break;;

                case  "mSQL":
                        $conn=msql_pconnect($host);
                        msql_select_db($db);
                        return $conn;
                        break;;

                case  "PostgreSQL":
                        $conn=pg_pconnect($host, "5432", "",$db);
                        return $conn;
                        break;;

                case  "ODBC":
                        $conn=odbc_pconnect($db,$user,$password);
                        return $conn;
                        break;;

                default:
                        $conn=mysql_pconnect($host, $user, $password);
                        mysql_select_db($db);
                        return $conn;
                        break;;
        }
}

// SQL_query($host, $user, $password, $db)
// executes an SQL statement, returns a result identifier
function SQL_query($query, $id)
{
        global $dbtype;
        switch ($dbtype) 
        {
                case  "MySQL":
                        $res=mysql_query($query, $id);
                        return $res;
                        break;;

                case  "mSQL":
                        $res=msql_query($query, $id);
                        return $res;
                        break;;

                case  "PostgreSQL":
                        $res=pg_exec($id,$query);
                        return $res;
                        break;;

                case  "ODBC":
                        $rid=odbc_prepare($id,$query);
                        $res=odbc_execute($rid);
                        return $res;
                        break;;

                default:
                        $res=mysql_query($query, $id);
                        return $res;
                        break;;
        }
}

// SQL_num_rows($host, $user, $password, $db)
// given a result identifier, returns the number of affected rows
function SQL_num_rows($res)
{
        global $dbtype;

        switch ($dbtype) 
        {
                case  "MySQL":
                        $rows=mysql_num_rows($res);
                        return $rows;
                        break;;

                case  "mSQL":
                        $rows=msql_num_rows($res);
                        return $rows;
                        break;;

                case  "PostgreSQL":
                        $rows=pg_numrows($res);
                        return $rows;
                        break;;

                case  "ODBC":
                        $rows=odbc_num_rows($res);
                        return $rows;
                        break;;

                default:
                        $rows=mysql_num_rows($res);
                        return $rows;
                        break;;
        }
}


// SQL_fetchrow($res,$row)
// given a result identifier, returns an array with the resulting row
// Needs also a row number for compatibility with PostgreSQL
function SQL_fetch_row($res, $nr)
{
        global $dbtype;

        switch ($dbtype) 
        {
                case  "MySQL":
                        $row = array();
                        $row = mysql_fetch_row($res);
                        return $row;
                        break;;

                case  "mSQL":
                        $row = array();
                        $row = msql_fetch_row($res);
                        return $row;
                        break;;

                case  "PostgreSQL":
                        $row = array();
                        $row = pg_fetch_row($res,$nr);
                        return $row;
                        break;;

                case  "ODBC":
                        $row = array();
                        $cols = odbc_fetch_into($res, $nr, &$row);
                        return $row;
                        break;;

                default:
                        $row = array();
                        $row = mysql_fetch_row($res);
                        return $row;
                        break;;
        }
}

// SQL_fetch_array($res,$row)
// given a result identifier, returns an associative array
// with the resulting row using field names as keys.
// Needs also a row number for compatibility with PostgreSQL.
function SQL_fetch_array($res, $nr)
{
        global $dbtype;

        switch ($dbtype) 
        {
                case  "MySQL":
                        $row = array();
                        $row = mysql_fetch_array($res);
                        return $row;
                        break;;

                case  "mSQL":
                        $row = array();
                        $row = msql_fetch_array($res);
                        return $row;
                        break;;

                case  "PostgreSQL":
                        $row = array();
                        $row = pg_fetch_array($res,$nr);
                        return $row;
                        break;;

                /*
                * ODBC doesn't have a native _fetch_array(), so we have to
                * use a trick. Beware: this might cause HUGE loads!
                */

                case  "ODBC":
                        $row = array();
                        $result = array();
                        $result = odbc_fetch_row($res, $nr);
                        $nf = count($result)+2;  /* Field numbering starts at 1 */
                        for ($count=1; $count < $nf; $count++) 
                        {
                                $field_name = odbc_field_name($res, $count);
                                $field_value = odbc_result($res, $field_name);
                                $row[$field_name] = $field_value;
                        }
                        return $row;
                        break;;
        }
}

13. ºÎ·Ï C PostgreSQL large object ¿¹Á¦

Á¦ÃâÀÚ : PHP code exchange px@sklar.com ÀÌÈ­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÁ®¿¡¼­ '´Ù¸¥À̸§À¸·Î ÀúÀå'À» ´­·¯ pgsql_largeobj.lib ¶ó°í ÀÔ·ÂÇϽÿÀ.



PX: PHP Code Exchange - PostgreSQL large object access

<?
        $database = pg_Connect ( "",  "",  "",  "",  "jacarta");
        pg_exec ($database,  "BEGIN");
        $oid = pg_locreate ($database);
        echo ( "$oid\n");
        $handle = pg_loopen ($database, $oid,  "w");
        echo ( "$handle\n");
        pg_lowrite ($handle,  "foo");
        pg_loclose ($handle);
        pg_exec ($database,  "COMMIT");
        pg_close ($database);
?>

14. ºÎ·Ï D »ç¿ëÀÚ ÀÎÁõ ¿¹Á¦

ÀÌÈ­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÁ®¿¡¼­ '´Ù¸¥À̸§À¸·Î ÀúÀå'À» ´©¸£°í user_pw.lib ¶ó°í ÀÔ·ÂÇϽÿÀ.

PHP 3 ¸Þ´º¾ó¿¡¼­ : PHP°¡ ¾ÆÆÄÄ¡ ¸ðµâÀ϶§¸¸ ÀÛµ¿ÇÑ´Ù. $PHP_AUTH_USER ¿Í $PHP_AUTH_PW ¸¦ Ãâ·ÂÇÏ´Â ´ë½Å, »ç¿ëÀÚÀ̸§°ú ºñ¹Ð¹øÈ£¸¦ È®Àθ¸ ÇϱâÀ» ¿øÇÒÁöµµ ¸ð¸¥´Ù. ¾Æ¸¶µµ µ¥ÀÌÅͺ£À̽º¿¡ Äõ¸®¸¦ º¸³»°Å³ª adm È­ÀÏ¿¡¼­ ã¾Æº¸¸é µÉ°ÍÀÌ´Ù.


<?php
        if (!$PHP_AUTH_USER)
        {
                Header("WWW-authenticate: basic realm=\"My Realm\"");
                Header("HTTP/1.0 401 Unauthorized");
                echo "Text to send if user hits Cancel button\n";
                exit;
        }
        else
        {
                echo "Hello $PHP_AUTH_USER.<P>";
                echo "You entered $PHP_AUTH_PW as your password.<P>";
        }
?>

15. ºÎ·Ï E ³×Æ®¿öÅ© °ü¸® ¿¹Á¦

ÀÌ È­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÀú¿¡¼­ network.lib¶õ À̸§ÀÇ 'ÅؽºÆ®(Text)' ÇüÅ·ΠÀúÀåÇ϶ó.

PHP: network adminstrator's best friend from http://www.phpWizard.net

À¥°³¹ßÀڷμ­ ¾Æ¸¶ ping, whois, nslookup µîµîÀÇ À¯¿ëÇÑ ÅøÀ» »ç¿ëÇÒ°ÍÀÌ´Ù. ±×·¯³ª °í°´ÀÇ »ç¹«½Ç¿¡¼­ ±× ÅøÁß Çϳª¸¦ »ç¿ëÇؾߵǴµ¥ telnetÀ» »ç¿ëÇÒ¼ö ¾ø´Ù¸é ¾î¶»°ÔÇÒ°ÍÀΰ¡? PHP ¸Å´º¾óÀÇ '³×Æ®¿öÅ©'¿¡¼­ functionÀ» ã¾Æº¸´Â °ÍÀÌ ´ë¾ÈÀÌ´Ù.

¼ÒÄÏ ÀÛµ¿ :

°¡Àå Áß¿äÇÑ ÇÔ¼ö´Â fsockopen()ÀÌ´Ù. ÀÌÇÔ¼ö¸¦ »ç¿ëÇÏ¸é ¼­¹öÀÇ ¿­·ÁÁø ¾î¶² Æ÷Æ®·Îµµ Á¢¼ÓÇÒ¼ö ÀÖ°í ¼ÒÄÏÀÇ ¿¬°áÀ» ¼³Á¤ÇÒ¼ö ÀÖ´Ù. ÇÔ¼öÀÇ ¹®¹ýÀº ´ÙÀ½°ú °°´Ù :


        int fsockopen(string hostname, int port, int [errno], string [errstr]);

¾ÕÀÇ 2°³ÀÇ º¯¼ö´Â ½±°Ô ¾Ë¼ö ÀÖ°í, ´ÙÀ½ÀÇ 2°³ÀÇ º¯¼ö´Â ¿É¼ÇÀÌ°í, ¿¡·¯¸¦ ó¸®Çϱâ À§ÇØ ¾²ÀδÙ. "errno"°ú "errstr"Àº ÂüÁ¶¸¦ ÅëÇؼ­ ÀÌ·ç¾îÁ®¾ß ÇÑ´Ù. "ÂüÁ¶¸¦ ÅëÇØ ÀÌ·ç¾îÁø´Ù"´Â ¸»ÀÇ Àǹ̴ ¿ø·¡ÀÇ º¯¼ö°¡ º¯ÇÑ´Ù´Â ¸»ÀÌ´Ù. º¸Åë, ÇÔ¼ö°¡ ½ÇÇàµÇ´õ¶óµµ º¯¼öÀÇ ³»¿ëÀº º¯ÇÏÁö ¾Ê´Â´Ù.

±×·¡¼­, À¥¼­¹ö¿¡ ¿¬°áÇϱâ À§ÇØ ÀÌÇÔ¼ö¸¦ »ç¿ëÇÏ°í Çì´õ¸¦ ÇÁ¸°Æ®Ç϶ó :


function get_headers($host, $path = "/")
{
        $fp = fsockopen ("$host", 80, &$errnr, &$errstr) or die("$errno: $errstr");
        fputs($fp,"GET $path HTTP/1.0\n\n");
        while (!$end)
        {
                $line = fgets($fp, 2048);
                if (trim($line) == "")
                        $end = true;
                else
                        echo $line;
        }
        fclose($fp);
}

ÀÌ ¿¹Á¦¿¡¼­´Â fsockopen()À» È£ÃâÇؼ­ ¾òÀº ÆÄÀÏÆ÷ÀÎÅÍ¿Í ÆÄÀÏ󸮿¡ °üÇÑÀÀ¿ëÀ» ¾Ë¾Æº¼°ÍÀÌ´Ù.(fred, fwrite µîµî) HTTP/1.0 Ŭ¶óÀ̾ðÆ®¿¡¼­¸¸ ±¸ÇöµÈ´Ù´Â°ÍÀ» ¸í½ÉÇ϶ó. - name-based °¡»óÈ£½ºÆ®¿¡¼­´Â µ¿ÀÛÇÏÁö ¾Ê´Â´Ù.

Áö½Ã:

ÀϹÝÀûÀ¸·Î ´Ù¸¥ Æ÷Æ®·Î ¿¬°á ÇÒ¼ö ÀÖ´Ù. PHP·Î ÀÛÀº ÇΰŠŬ¶óÀ̾ðÆ®¸¦ ¸¸µç°ÍÀº ½¬°Ô ÇÒ¼ö ÀÖ´Ù. ÇΰŠµ¥¸ó¿¡ Äõ¸®(Áú¹®)Çϱâ À§ÇØ ¾Æ·¡¿¡ ÀÖ´Â ¿¹Á¦¸¦ ¼öÁ¤Ç϶ó.


function finger ($host, $user)
{
        $fp = fsockopen($host, 79, &$errno, &$errstr) or die("$errno: $errstr");
        fputs($fp, "$user\n");
        while (!feof($fp))
                echo fgets($fp, 128);
        fclose($fp);
}

Whois: ÈÄÀ̽º(whois) ¼­¹ö¿¡ Áú¹®Çϴ°͵µ °°Àº °³³äÀÌ´Ù:


// domain is like "phpwizard.net"
function whois($domain, $server="whois.internic.net")
{
        $fp = fsockopen ($server, 43, &$errnr, &$errstr) or die("$errno: $errstr");
        fputs($fp, "$domain\n");
        while (!feof($fp))
                echo fgets($fp, 2048);
        fclose($fp);
}

ºí·°Å·, ³Íºí·ÏÅ· Á¦¾î : ±×·¯³ª ÀÌÇÔ¼ö¸¦ »ç¿ëÇϴµ¥ ¹®Á¦°¡ ÀÖ´Ù. ´ÙÀ½°ú °°À»¶§¸¸ ÀßÀÛµ¿ÇÑ´Ù.

  1. È£Ãâ½Ã°£ÀÌ ÀÛÀº ¿¬°áÀ϶§¿Í
  2. ¿¬°áÇÏ°í ÀÖ´Â ¼­¹ö°¡ »ì¾ÆÀÖ°í ÀÛµ¿ÇÒ¶§. ±×·¸Áö ¾ÊÀ¸¸é, ´ç½ÅÀÇ ½ºÅ©¸³Æ®´Â Á¾·áµÉ¶§±îÁö ÀÛµ¿ÇÑ´Ù. ±×·¸°Ô µÇ´Â ÀÌÀ¯´Â ¼ÒÄÏ¿¬°áÀº ºí·°Å·µÇ¸ç Á¾·á°¡ µÇÁö¾Ê±â ¶§¹®ÀÌ´Ù. set_socket_blocking()À̶õ ÇÔ¼ö´Â ´ÙÀ½°ú °°ÀÌ µ¿ÀÛÇÑ´Ù: ÇÔ¼ö´Â ¸ðµçµ¿ÀÛÀ» ¼ÒÄÏ(ù¹ø° Àμö:¼ÒÄÏ Æ÷ÀÎÅÍ)¿¡ ºí·°Å·(µÎ¹øÀç Àμö:true)³ª Æú½º(false)(¼¼¹ø° Àμö:false)·Î ¼³Á¤ÇÑ´Ù. ³Íºí·°Å·Á¦¾î¸¦ »ç¿ëÇϸé, ÇΰÅ(finger)ÇÔ¼ö´Â ´ÙÀ½°ú °°À»°ÍÀÌ´Ù:(¿ªÁÖ:¹«½¼¸»ÀÎÁö ¹ø¿ªÀÌ ÀÌ»óÇϳ׿ä)


        $fp = fsockopen($host, 79, &$errno, &$errstr) or die("$errno: [ ] $errstr");
        set_socket_blocking($fp, 0);
        fputs($fp, "$user\n");

        $stop = time() + $timeout;
        while (!feof($fp) && time() < $stop )
                echo fgets($fp, 128);
        fclose($fp);

³Íºí·¯Å· ¼ÒÄÏÄÝ(socket calls)À» »ç¿ëÇϱâ À§ÇÑ 3°³ÀÇ ÇÔ¼öÀÇ ¼öÁ¤Àº ´ç½ÅÀÇ ¿¬½ÀÀ» À§ÇØ ³²°ÜµÐ´Ù.

16. ºÎ·Ï F PostgreSQL Database Wrapper ¿¹Á¦

Á¦ÃâÀÚ : Joe Thong darkjoe@softhome.net »çÀÌÆ® URL: http://phpdb.linuxbox.com

¼³¸í: ¿©·¯°¡Áö µ¥ÀÌÅͺ£À̽º ¼­¹ö¿¡¼­ °á°ú Á¶ÀÛÀ» À§ÇÑ °­·ÂÇÑ PHP µ¥ÀÌŸº£À̽º ·¦ÆÛ. µ¥ÀÌÅͺ£À̽ºÀÇ °á°ú°¡ phpDB¿¡ ÀÇÇØ ÀÚµ¿À¸·Î ³ª¿À°Ô µÈ´Ù.

ÀÌÈ­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÁ®¿¡¼­ '´Ù¸¥À̸§À¸·Î ÀúÀå'À» ´©¸£°í phpDB-postgresql.lib ¶ó°í ÀÔ·ÂÇϽÿÀ.


<?php
/*
Name: phpDB PostgreSQL module
Version: 1.02bR6
Description: A PHP database wrapper for various database 
        servers with a powerful recordset for result data 
        manipulation.  Database results are flushed 
        automatically by phpDB.  
*/

/* define this module, to prevent double class declaration.     */
if (!defined("_PHPDB_ABSTRACT_LAYER")) {
        define("_PHPDB_ABSTRACT_LAYER", 1 );
}
else
        return;

//---------------------------------------
         Class Name: phpDB
//---------------------------------------
class phpDB 
{
        /*      public variables        */
        var $version = '1.02bR6'; // Version number of phpDB
        // This variable keeps what database type is going to 
        // be used.  Current supported database server are 
        // MySQL, MSQL, SQL Server, and Sybase
        var $databaseType = '';
        // Specifies which database is going to be used
        var $databaseName = ''; 
        // The hostname of the database server, port 
        // number is optional.  e.g: "db.devNation.com"
        var $hostname = '';     
        var $username = ''; // used to connect to the database server
        var $password = ''; // Password for the username

        // Private variables ------ starts with underscore
        // An array of executed querys. For results cleanup purposes.
        var $_queryIDList = array();
        // The returned link identifier whenever a 
        // successful database connection is made
        var $_connectionID      = -1;   
        // A variable which was used to keep the returned 
        // last error message.  The value will then returned 
        // by the errorMsg() function
        var $_errorMsg = '';
                                                
        // This variable keeps the last created result 
        // link identifier
        var $_queryID = -1;     
        // A boolean variable to state whether its a persistent
        // connection or normal connection
        var     $_isPersistentConnection = false;
        // Holds the newly created result object, 
        // returned via the execute() method
        var $_tempResultObj = '';       

        // A constructor function for the phpDB object.  
        // When initializing, specify the dbType i.e: "mysql",
        // "msql", "postgresql", "mssql", and "sybase"
        function phpDB($dbType = "postgresql") 
        {
                switch ($dbType) {
                        case "mysql":
                        case "msql":
                        case "postgresql":
                        case "mssql":
                        case "sybase":                                          
                        case "informix":                                                
                                $this->databaseType = $dbType;
                                break;
                        default:
                                return false;
                }                       
        }
        
        // Returns: A positive link identifier on success, or 
        // false on error.      Connect to the server with the provided 
        // arguments. The connection to the server will be closed 
        // when the script terminates, unless close() function is 
        // called beforehand
        function connect($argHostname = "", $argUsername = "", 
                $argPassword = "", $argDatabaseName = "") 
        {
                $connString = "";
                $hostPieces = array();
                /*      Must specify the database argument      */
                if (!$argDatabaseName) {        
                        return false;
                }
                if ($argHostname != "") {
                        $this->hostname = $argHostname;
                }
                if ($argUsername != "") {
                        $this->username = $argUsername;
                }
                if ($argPassword != "") {
                        $this->password = $argPassword;
                }
                if ($argDatabaseName != "") {
                        $this->databaseName = $argDatabaseName;
                }                               
                $hostPieces = split(":", $this->hostname);
                if ($hostPieces[0]) {
                        $connString .= "host=$hostPieces[0]";
                        if (isset($hostPieces[1])) {
                                $connString .= " port=$hostPieces[1]";
                        }                       
                }
            if ($this->username) {
                        $connString .= " user=$this->username";
                }
            if ($this->password) {
                        $connString .= " password=$this->password";
                }
                $connString .= " dbname=$this->databaseName";
                        
                $this->_connectionID = @pg_Connect($connString);
                return $this->_connectionID;
        }

        // Returns: A positive link identifier on success, or
        // false on error.  Connect to the server with the 
        // provided arguments. The connection to the server will 
        // not be closed when the script terminates. Instead it 
        // will be kept for later future use
        function pconnect($argHostname = "", $argUsername = "", 
                $argPassword = "", $argDatabaseName = "") 
        {
                $connString = "";
                $hostPieces = array();          
                /*      Must specify the database argument      */
                if (!$argDatabaseName) {
                        return false;
                }
                if ($argHostname != "") {
                        $this->hostname = $argHostname;
                }
                if ($argUsername != "") {
                        $this->username = $argUsername;
                }
                if ($argPassword != "") {
                        $this->password = $argPassword;
                }
                if ($argDatabaseName != "") {
                        $this->databaseName = $argDatabaseName;
                }                               
                $hostPieces = split(":", $this->hostname);
                if ($hostPieces[0]) {
                        $connString .= "host=$hostPieces[0]";
                        if (isset($hostPieces[1])) {
                                $connString .= " port=$hostPieces[1]";
                        }                       
                }
            if ($this->username) {
                        $connString .= " user=$this->username";
                }
            if ($this->password) {
                        $connString .= " password=$this->password";
                }
                $connString .= " dbname=$this->databaseName";
                        
                $this->_connectionID = @pg_pConnect($connString);
                if ($this->_connectionID) {
                        $this->_isPersistentConnection = true;                          
                }
                return $this->_connectionID;
        }               
        
        // Returns: true on success, false on error Select 
        // the database name to be used PostgreSQL 
        // Note:        function Not available
        function selectDB($dbName) {
                        return false;
        }

        // Returns: the Recordset object disregard success 
        // or failure Send the sql statement to the database server
        function execute($sql = "") {
                // Instantiate an object without considering whether 
                // the query return any results or not
                $this->_queryID = @pg_Exec($this->_connectionID, $sql);
                $this->_tempResultObj = new Recordset($this->_queryID);
                $this->_insertQuery($this->_queryID);
                return $this->_tempResultObj;
        }
        
        // Returns: the last error message from previous 
        // database operation
        function errorMsg() {
                $this->_errorMsg = @pg_errormessage($this->_connectionID);
            return $this->_errorMsg;
        }

        // Returns: true on success, false on failure 
        // Close the database connection
        function close() {
                if ($this->_queryIDList && sizeof($this->_queryIDList > 0)) {
                        while(list($_key, $_resultID) = each($this->_queryIDList)) {
                                @pg_freeresult($_resultID);
                        }
                }
                // If its not a persistent connection, then 
                // only the connection needs to be closed
                if ($this->_isPersistentConnection != true) {   
                        return @pg_close($this->_connectionID);
                }
                else {
                        return true;
                }
        }
        
        // A PRIVATE function used by the constructor function 
        // of the query object.  insert the successful returned
        // query id to the query id list.  Used for later results 
        // cleanup.  A private function that's never meant to 
        // be used directly
        function _insertQuery($query_id) {
                $this->_queryIDList[] = $query_id;
        }               
}
        
//------------------------------
         Class Name: Recordset
//------------------------------
class Recordset 
{
        /*      public variables        */
        var $fields;
        // indicates that the current record position is before 
        // the first record in a Recordset object
        var $BOF = null;

        // indicates that the current record position is after
        // the last record in a Recordset object
        var $EOF = null;        
        
        /*      private variables       */
        var $_numOfRows = -1; // NEVER change the value! READ-ONLY!
        var $_numOfFields = -1; // NEVER change the value! READ-ONLY!
        // Holds anything that was returned from the database specific functions
        var $_tempResult = ''; 
        // This variable keeps the result link identifier
        var $_queryID = -1;     
        // This variable keeps the current row in the Recordset
        var $_currentRow = -1;

        // Returns: query id on success and false if 
        // failed Constructor function
        function Recordset($queryID) 
        {
                $this->_queryID = $queryID;
                if ($queryID) {
                        $this->_numOfRows = @pg_numrows($this->_queryID);
                        /*      pg_numrows() returns -1 on error        */
                        if ($this->_numOfRows == -1) {
                                $this->_numOfRows = 0;
                        }
                        $this->_numOfFields = @pg_numfields($this->_queryID);
                        /*      pg_numfields() returns -1 on error      */
                        if ($this->_numOfFields == -1) {
                                $this->_numOfFields = 0;
                        }
                }
                else {
                        $this->_numOfRows = 0;
                        $this->_numOfFields = 0;
                }
                /*      If result set contains rows     */
                if ($this->_numOfRows > 0 && $this->_currentRow == -1) {
                        $this->_currentRow = 0;
                        $this->fields = @pg_fetch_array($this->_queryID, $this->_currentRow);                   
                        $this->EOF = false;
                        $this->BOF = false;
                }
                return $this->_queryID;
        }
                
        // Returns: true if successful, false if fail Set the Recordset 
        // pointer to a specified field offset. If the next call to 
        // fetchField() won't include a field offset, this field would 
        // be returned.  PostgreSQL Note:       function Not available
        function fieldSeek($fieldOffset = -1) {
                $this->_tempResult = false;
                return $this->_tempResult;
        }

        // Returns: an object containing field information.  Get column 
        // information in the Recordset object. fetchField() can be used 
        // in order to obtain information about fields in a certain query 
        // result. If the field offset isn't specified, the next field 
        // that wasn't yet retrieved by fetchField() is retrieved. 
        // PostgreSQL Note:     function Not available
        function fetchField($fieldOffset = -1) {
                $this->_tempResult = false;             
                return $this->_tempResult;
        }                               

        // Returns: true if there still rows available, or false if there 
        // are no more rows.  Moves to the next row in a specified Recordset 
        // object and makes that record the current row and the data 
        // corresponding to the row will be retrieved into the fields 
        // collection.  Note: Unlike the moveRow() method, when _currentRow
        // is getNumOfRows() - 1, EOF will immediately be true.  If row number
        // is not provided, the function will point to the 
        // first row automatically
        function nextRow() {
                if ($this->getNumOfRows() > 0) {                        
                        $this->fields = array();
                        $this->_currentRow++;
                        $this->fields = @pg_fetch_array($this->_queryID, $this->_currentRow);
                        /*      This is not working.  True all the time */
                        if ($this->fields) {
                                $this->_checkAndChangeEOF($this->_currentRow - 1);
                                return true;
                        }
                }
                $this->EOF = true;
                return false;
        }       
                        
        // Returns: true on success, false on failure moveRow() moves 
        // the internal row pointer of the Recordset object to point 
        // to the specified row number and the data corresponding to
        // the row will be retrieved into the fields collection.  If 
        // row number is not provided, the function will point to 
        // the first row automatically
        function moveRow($rowNumber = 0) {
                if ($rowNumber == 0) {
                        return $this->firstRow();
                }
                else if ($rowNumber == ($this->getNumOfRows() - 1)) {
                        return $this->lastRow();
                }
                if ($this->getNumOfRows() > 0 && $rowNumber < $this->getNumOfRows()) {       
                        $this->fields = null;
                        $this->_currentRow = $rowNumber;
                        $this->fields = @pg_fetch_array($this->_queryID, $this->_currentRow);
                        /*      This is not working.  True all the time */
                        if ($this->fields) {
                                // No need to call _checkAndChangeEOF() because 
                                // the possibility of moving to the last row 
                                // has been handled by the above code
                                $this->EOF = false;
                                return true;
                        }
                }
                $this->EOF = true;
                return false;
        }

        // Returns: true on success, false on failure firstRow() 
        // moves the internal row pointer of the Recordset object
        // to the first row and the data corresponding to the row 
        // will be retrieved into the fields collection
        function firstRow() {
                if ($this->getNumOfRows() > 0) {
                        $this->fields = array();
                        $this->_currentRow = 0;
                        $this->fields = @pg_fetch_array($this->_queryID, $this->_currentRow);
                        $this->EOF = true;
                        /*      This is not working.  True all the time */
                        if ($this->fields) {
                                return true;    
                        }
                }
                $this->EOF = true;
                return false;           
        }

        // Returns: true on success, false on failure lastRow() 
        // moves the internal row pointer of the Recordset object 
        // to the last row and the data corresponding to the row 
        // will be retrieved into the fields collection
        function lastRow() {
                if ($this->getNumOfRows() > 0) {        
                        $this->fields = array();        
                        $num_of_rows = $this->getNumOfRows();
                        /*      $num_of_rows decemented at above        */
                        $this->_currentRow = --$num_of_rows;    
                        $this->fields = @pg_fetch_array($this->_queryID, $this->_currentRow);
                        /*      This is not working.  True all the time */
                        if ($this->fields) {
                                /*      Special case for making EOF false.      */
                                $this->EOF = false;     
                                return true;
                        }
                }
                $this->EOF = true;
                return false;
        }

        // close() only needs to be called if you are worried about 
        // using too much memory while your script is running. All 
        // associated result memory for the specified result identifier 
        // will automatically be freed
        function close() {
                $this->_tempResult = @pg_freeresult($this->_queryID);           
                return $this->_tempResult;
        }

        // Returns: the number of rows in a result set.  
        // Get number of rows in result
        function getNumOfRows() {
                return $this->_numOfRows;
        }

        // Returns: the number of fields in a result set.
        // Get number of fields in result
        function getNumOfFields() {
                return $this->_numOfFields;
        }       

        /*      Check and change the status of EOF.     */              
        function _checkAndChangeEOF($currentRow) {
                if ($currentRow >= ($this->_numOfRows - 1)) {
                        $this->EOF = true;
                }               
                else {
                        $this->EOF = false;             
                }
        }
}
?>

17. ºÎ·Ï G Microsoft SQL Server DB Wrapper ¿¹Á¦

Á¦ÃâÀÚ : Joe Thong darkjoe@softhome.net Site URL: http://phpdb.linuxbox.com

¼³¸í: ¿©·¯°¡Áö µ¥ÀÌÅͺ£À̽º ¼­¹ö¿¡¼­ °á°ú Á¶ÀÛÀ» À§ÇÑ °­·ÂÇÑ PHP µ¥ÀÌŸº£À̽º ·¦ÆÛ. µ¥ÀÌÅͺ£À̽ºÀÇ °á°ú°¡ phpDB¿¡ ÀÇÇØ ÀÚµ¿À¸·Î ³ª¿À°Ô µÈ´Ù.

ÀÌÈ­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÁ®¿¡¼­ '´Ù¸¥À̸§À¸·Î ÀúÀå'À» ´©¸£°í phpDB-msql.lib ¶ó°í ÀÔ·ÂÇϽÿÀ.


<?php
/*
Name: phpDB Microsoft SQL Server module
Version: 1.02bR6
Description: A PHP database wrapper for various 
        database servers with a powerful
        Recordset for result data manipulation.  Database 
        results are flushed automatically by phpDB.  
*/
// Define this module, to prevent double class declaration
if (!defined("_PHPDB_ABSTRACT_LAYER")) 
{
        define("_PHPDB_ABSTRACT_LAYER", 1 );
}
else
        return;

//------------------------------
         Class Name: phpDB
//------------------------------
class phpDB 
{
        // public variables
        var $version = '1.02bR6'; // Version number of phpDB

        // This variable keeps what database type is going 
        // to be used.  Current supported database server 
        // are MySQL, MSQL, SQL Server, PostgreSQL and Sybase
        var $databaseType = ''; 
        var $databaseName = ''; // Specifies which database is going to be used

        // The hostname of the database server, port 
        // number is optional.  e.g: "db.devNation.com"
        var $hostname = ''; 

        var $username = ''; // to connect to the database server
        var $password = '';     // Password for the username
        
        // Private variables ----- starts with underscore

        // An array of executed querys. For results cleanup purposes
        var $_queryIDList = array();

        // The returned link identifier whenever a 
        // successful database connection is made
        var $_connectionID      = -1;
                
        // A variable which was used to keep the returned last 
        // error message.  The value will then returned 
        // by the errorMsg() function
        var $_errorMsg = ''; 
        // This variable keeps the last created result link identifier
        var $_queryID = -1;     

        // A boolean variable to state whether its a 
        // persistent connection or normal connection
        var     $_isPersistentConnection = false;       
        
        // Holds the newly created result object, returned 
        // via the execute() method
        var $_tempResultObj = '';       

        //      A constructor function for the phpDB object.  
        // When initializing, specify the dbType i.e: "mysql",
        // "msql", "postgresql", "mssql", and "sybase"
        function phpDB($dbType = "mssql") 
        {
                switch ($dbType) 
                {
                        case "mysql":
                        case "msql":
                        case "postgresql":
                        case "mssql":
                        case "sybase":                                          
                        case "informix":                                                
                                $this->databaseType = $dbType;
                                break;
                        default:
                                return false;
                }
        }

        // Returns: A positive link identifier on success, 
        // or false on error.   Connect to the server with 
        // the provided arguments. The connection to the server 
        // will be closed when the script terminates, unless 
        // close() function is called beforehand.
        function connect($argHostname = "", $argUsername = "", 
                $argPassword = "", $argDatabaseName = "") 
        {
                $boolDBSelected;
                if ($argHostname != "") {
                        $this->hostname = $argHostname;
                }
                if ($argUsername != "") {
                        $this->username = $argUsername;
                }
                if ($argPassword != "") {
                        $this->password = $argPassword;
                }
                if ($argDatabaseName != "") {
                        $this->databaseName = $argDatabaseName;
                }                               
                
                $this->_connectionID = @mssql_connect($this->hostname, $this->username, $this->password);
                
                if ($this->databaseName && $this->_connectionID) {
                        $boolDBSelected = @mssql_select_db($this->databaseName);
                        if(!$boolDBSelected) {  /*      If DB selection fails   */
                                @mssql_close($this->_connectionID);     /*      Close the current connection    */
                                return false;   
                        }
                }               
                return $this->_connectionID;
        }

        // Returns: A positive link identifier on success, or 
        // false on error Connect to the server with the provided
        // arguments. The connection to the server will not be closed 
        // when the script terminates. Instead it will be kept for 
        // later future use
        function pconnect($argHostname = "", $argUsername = "", 
                $argPassword = "", $argDatabaseName = "") 
        {
                $boolDBSelected;
                if ($argHostname != "") {
                        $this->hostname = $argHostname;
                }
                if ($argUsername != "") {
                        $this->username = $argUsername;
                }
                if ($argPassword != "") {
                        $this->password = $argPassword;
                }
                if ($argDatabaseName != "") {
                        $this->databaseName = $argDatabaseName;
                }                               
                
                $this->_connectionID = @mssql_pconnect($this->hostname, $this->username, $this->password);
                if ($this->_connectionID) {
                        $this->_isPersistentConnection = true;                          
                }
                
                if ($this->databaseName && $this->_connectionID) {
                        $boolDBSelected = @mssql_select_db($this->databaseName);
                        if(!$boolDBSelected) {  /*      if DB selection fails   */
                                return false;   /*      Persistent connection can't be closed   */
                        }
                }               
                return $this->_connectionID;
        }
        
        //      Returns: true on success, false on error Select the 
        // database name to be used
        function selectDB($dbName) 
        {
                $this->databaseName = $dbName;
                if ($this->_connectionID) {
                        return @mssql_select_db($dbName);               
                }
                else {
                        /*      No database selected    */
                        return false;
                }                       
        }

        // Returns: the Recordset object disregard success or 
        // failure Send the sql statement to the database server
        function execute($sql = "") 
        {
                $this->_queryID = @mssql_query($sql, $this->_connectionID);
                // Instantiate an object without considering whether 
                // the query return any results or not
                $this->_tempResultObj = new Recordset($this->_queryID);
                $this->_insertQuery($this->_queryID);
                return $this->_tempResultObj;
        }

        // Returns: the last error message from previous database
        // operation Note: This function is NOT available for
        // Microsoft SQL Server
        function errorMsg() 
        {
                $this->_errorMsg = "errorMsg() is not available for Microsoft SQL Server";
            return $this->_errorMsg;
        }

        /*      Returns: true on success, false on failure
                Close the database connection.  */      
        function close() {
                if ($this->_queryIDList && sizeof($this->_queryIDList > 0)) {
                        while(list($_key, $_resultID) = each($this->_queryIDList)) {
                                @mssql_free_result($_resultID);
                        }
                }
                // If its not a persistent connection, then 
                // only the connection needs to be closed
                if ($this->_isPersistentConnection != true) {   
                        return @mssql_close($this->_connectionID);
                }
                else {
                        return true;
                }
        }
        
        // A PRIVATE function used by the constructor function of 
        // the query object.  insert the successful returned 
        // query id to the query id list.  Used for later results
        // cleanup.  A private function that's never meant to be 
        // used directly
        function _insertQuery($query_id) {
                $this->_queryIDList[] = $query_id;
        }               
}

//---------------------------------------
         Class Name: Recordset
//---------------------------------------
class Recordset 
{
        /*      public variables        */
        var $fields;
        // indicates that the current record position is 
        // before the first record in a Recordset object
        var $BOF = null;
        // indicates that the current record position is 
        // after the last record in a Recordset object
        var $EOF = null;

        // Private variables
        var $_numOfRows = -1; // NEVER change the value!  READ-ONLY!
        var $_numOfFields = -1; // NEVER change the value!  READ-ONLY!
        
        // Holds anything that was returned from the 
        // database specific functions
        var $_tempResult = '';  
        // This variable keeps the result link identifier
        var $_queryID = -1;     
        // This variable keeps the current row in the Recordset
        var $_currentRow = -1;

        // Returns: query id on success and false if 
        // failed Constructor function
        function Recordset($queryID) 
        {
                $this->_queryID = $queryID;
                if ($queryID) {
                        $this->_numOfRows = @mssql_num_rows($this->_queryID);
                        $this->_numOfFields = @mssql_num_fields($this->_queryID);
                }
                else {
                        $this->_numOfRows = 0;
                        $this->_numOfFields = 0;
                }
                // If result set contains rows  
                if ($this->_numOfRows > 0 && $this->_currentRow == -1) {
                        $this->_currentRow = 0;
                        $this->fields = @mssql_fetch_array($this->_queryID);
                        $this->EOF = false;
                        $this->BOF = false;
                }
                return $this->_queryID;
        }
                
        // Returns: true if successful, false if fail Set 
        // the Recordset pointer to a specified field offset. 
        // If the next call to fetchField() won't include a 
        // field offset, this field would be returned
        function fieldSeek($fieldOffset = -1) {
                $this->_tempResult = @mssql_field_seek($this->_queryID, $fieldOffset);
                return $this->_tempResult;
        }

        // Returns: an object containing field information. 
        // Get column information in the Recordset object. 
        // fetchField() can be used in order to obtain information
        // about fields in a certain query result. If the field 
        // offset isn't specified, the next field that wasn't yet 
        // retrieved by fetchField() is retrieved
        function fetchField($fieldOffset = -1) {
                if ($fieldOffset != -1) {
                        $this->_tempResult = @mssql_fetch_field($this->_queryID, $fieldOffset);
                }
                // The $fieldOffset argument is not provided thus its -1
                else if ($fieldOffset == -1) {  
                        $this->_tempResult = @mssql_fetch_field($this->_queryID);
                }
                return $this->_tempResult;
        }

        // Returns: true if there still rows available, or false 
        // if there are no more rows.  Moves to the next row in a 
        // specified Recordset object and makes that record the current 
        // row and the data corresponding to the row will be retrieved
        // into the fields collection.  Note: Unlike the moveRow() method, 
        // when _currentRow is getNumOfRows() - 1, EOF will immediately be 
        // true.  If row number is not provided, the function will point 
        // to the first row automatically
        function nextRow() 
        {
                if ($this->getNumOfRows() > 0) {                        
                        $this->fields = array();
                        $this->_currentRow++;
                        $this->fields = @mssql_fetch_array($this->_queryID);
                        // This is not working.  True all the time
                        if ($this->fields) {    
                                $this->_checkAndChangeEOF($this->_currentRow - 1);
                                return true;
                        }
                }
                $this->EOF = true;
                return false;
        }       
                        
        // Returns: true on success, false on failure moveRow() 
        // moves the internal row pointer of the Recordset object 
        // to point to the specified row number and the data 
        // corresponding to the row will be retrieved into the fields 
        // collection.  If row number is not provided, the function will 
        // point to the first row automatically
        function moveRow($rowNumber = 0) 
        {
                if ($rowNumber == 0) {
                        return $this->firstRow();
                }
                else if ($rowNumber == ($this->getNumOfRows() - 1)) {
                        return $this->lastRow();
                }
                if ($this->getNumOfRows() > 0 && $rowNumber < $this->getNumOfRows()) {       
                        $this->fields = null;
                        $this->_currentRow = $rowNumber;
                        if(@mssql_data_seek($this->_queryID, $this->_currentRow)) {
                                $this->fields = @mssql_fetch_array($this->_queryID);
                                /*      This is not working.  True all the time */
                                if ($this->fields) {
                                        // No need to call _checkAndChangeEOF() because
                                        // the possibility of moving to the last row has 
                                        // been handled by the above code
                                        $this->EOF = false; 
                                        return true;
                                }
                        }
                }
                $this->EOF = true;
                return false;
        }

        // Returns: true on success, false on failure firstRow() moves
        // the internal row pointer of the Recordset object to the first 
        // row and the data corresponding to the row will be retrieved 
        // into the fields collection
        function firstRow() 
        {
                if ($this->getNumOfRows() > 0) {
                        $this->fields = array();
                        $this->_currentRow = 0;
                        if (@mssql_data_seek($this->_queryID, $this->_currentRow)) {
                                $this->fields = @mssql_fetch_array($this->_queryID);
                                $this->EOF = false;
                                /*      This is not working.  True all the time */
                                if ($this->fields) {    
                                        return true;    
                                }
                        }
                }
                $this->EOF = true;
                return false;           
        }

        // Returns: true on success, false on failure lastRow() moves 
        // the internal row pointer of the Recordset object to the last
        // row and the data corresponding to the row will be retrieved 
        // into the fields collection
        function lastRow() 
        {
                if ($this->getNumOfRows() > 0) {        
                        $this->fields = array();        
                        $num_of_rows = $this->getNumOfRows();
                        $this->_tempResult = @mssql_data_seek($this->_queryID, --$num_of_rows);
                        if ($this->_tempResult) {
                                /*      $num_of_rows decemented at above        */
                                $this->_currentRow = $num_of_rows;
                                $this->fields = @mssql_fetch_array($this->_queryID);
                                /*      This is not working.  True all the time */
                                if ($this->fields) {
                                        /*      Special case for making EOF false.      */
                                        $this->EOF = false;     
                                        return true;
                                }
                        }
                }
                $this->EOF = true;
                return false;
        }

        // close() only needs to be called if you are worried about using
        // too much memory while your script is running. All associated 
        // result memory for the specified result identifier will 
        // automatically be freed
        function close() {
                $this->_tempResult = @mssql_free_result($this->_queryID);               
                return $this->_tempResult;
        }

        // Returns: the number of rows in a result set.  Get 
        // number of rows in result
        function getNumOfRows() {
                return $this->_numOfRows;
        }

        /*      Returns: the number of fields in a result set. 
        Get number of fields in result. */
        function getNumOfFields() {
                return $this->_numOfFields;
        }       

        /*      Check and change the status of EOF.     */              
        function _checkAndChangeEOF($currentRow) {
                if ($currentRow >= ($this->_numOfRows - 1)) {
                        $this->EOF = true;
                }               
                else {
                        $this->EOF = false;             
                }
        }
}
?>

18. ºÎ·Ï H Sybase SQL Server DB Wrapper ¿¹Á¦

Á¦ÃâÀÚ : Joe Thong darkjoe@softhome.net Site URL: http://phpdb.linuxbox.com

¼³¸í: ¿©·¯°¡Áö µ¥ÀÌÅͺ£À̽º ¼­¹ö¿¡¼­ °á°ú Á¶ÀÛÀ» À§ÇÑ °­·ÂÇÑ PHP µ¥ÀÌŸº£À̽º ·¦ÆÛ. µ¥ÀÌÅͺ£À̽ºÀÇ °á°ú°¡ phpDB¿¡ ÀÇÇØ ÀÚµ¿À¸·Î ³ª¿À°Ô µÈ´Ù.

ÀÌÈ­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÁ®¿¡¼­ '´Ù¸¥À̸§À¸·Î ÀúÀå'À» ´©¸£°í phpDB-sybase.lib ¶ó°í ÀÔ·ÂÇϽÿÀ.


<?php
/*
Name: phpDB Sybase module
Version: 1.02bR6
Description: A PHP database wrapper for various database 
        servers with a powerful Recordset for result data 
        manipulation.  Database results are flushed 
        automatically by phpDB.  
*/

// Define this module, to prevent double class declaration
if (!defined("_PHPDB_ABSTRACT_LAYER")) {
        define("_PHPDB_ABSTRACT_LAYER", 1 );
}
else
        return;

//-------------------------------
         Class Name: phpDB
//-------------------------------
class phpDB 
{
        /*      public variables        */
        var $version = '1.02bR6'; // Version number of phpDB
        // This variable keeps what database type is going 
        // to be used.  Current supported database server 
        // are MySQL, MSQL, SQL Server, and Sybase
        var $databaseType = ''; 
        // Specifies which database is going to be used
        var $databaseName = ''; 
        // The hostname of the database server, port number 
        // is optional.  e.g: "db.devNation.com"
        var $hostname = '';
        var $username = '';     // to connect to the database server
        var $password = ''; // Password for the username

        // Private variables --- starts with underscore
        // An array of executed querys.  For results 
        // cleanup purposes
        var $_queryIDList       = array();      
        // The returned link identifier whenever a successful 
        // database connection is made
        var $_connectionID      = -1;   
        // A variable which was used to keep the returned last 
        // error message.  The value will then returned by 
        // the errorMsg() function
        var $_errorMsg = '';
        // This variable keeps the last created result 
        // link identifier
        var $_queryID = -1;     
        // A boolean variable to state whether its a 
        // persistent connection or normal connection
        var     $_isPersistentConnection = false;       
        // Holds the newly created result object, returned 
        // via the execute() method
        var $_tempResultObj = '';

        // A constructor function for the phpDB object.  When 
        // initializing, specify the dbType i.e: "mysql", 
        // "msql", "postgresql", "mssql", and "sybase"
        function phpDB($dbType = "sybase") 
        {
                switch ($dbType) {
                        case "mysql":
                        case "msql":
                        case "postgresql":
                        case "mssql":
                        case "sybase":                                          
                        case "informix":                                                
                                $this->databaseType = $dbType;
                                break;
                        default:
                                return false;
                }                       
        }
        
        // Returns: A positive link identifier on success, or
        // false on error.      Connect to the server with the 
        // provided arguments. The connection to the server will be 
        // closed when the script terminates, unless close()
        // function is called beforehand
        function connect($argHostname = "", $argUsername = "", 
                $argPassword = "", $argDatabaseName = "") 
        {
                $boolDBSelected;
                if ($argHostname != "") {
                        $this->hostname = $argHostname;
                }
                if ($argUsername != "") {
                        $this->username = $argUsername;
                }
                if ($argPassword != "") {
                        $this->password = $argPassword;
                }
                if ($argDatabaseName != "") {
                        $this->databaseName = $argDatabaseName;
                }                               
                
                $this->_connectionID = @sybase_connect($this->hostname, $this->username, $this->password);
                
                if ($this->databaseName && $this->_connectionID) {
                        $boolDBSelected = @sybase_select_db($this->databaseName);
                        /*      If DB selection fails   */
                        if(!$boolDBSelected) {  
                                /*      Close the current connection    */
                                @sybase_close($this->_connectionID);
                                return false;   
                        }
                }               
                return $this->_connectionID;
        }

        // Returns: A positive link identifier on success, or false 
        // on error.  Connect to the server with the provided 
        // arguments. The connection to the server will not be closed
        // when the script terminates. Instead it will be kept for later future use
        function pconnect($argHostname = "", $argUsername = "", 
                $argPassword = "", $argDatabaseName = "") 
        {
                $boolDBSelected;
                if ($argHostname != "") {
                        $this->hostname = $argHostname;
                }
                if ($argUsername != "") {
                        $this->username = $argUsername;
                }
                if ($argPassword != "") {
                        $this->password = $argPassword;
                }
                if ($argDatabaseName != "") {
                        $this->databaseName = $argDatabaseName;
                }                               
                
                $this->_connectionID = @sybase_pconnect($this->hostname, $this->username, $this->password);
                if ($this->_connectionID) {
                        $this->_isPersistentConnection = true;                          
                }
                
                if ($this->databaseName && $this->_connectionID) {
                        $boolDBSelected = @sybase_select_db($this->databaseName);
                        /*      if DB selection fails   */
                        if(!$boolDBSelected) {
                                /*      Persistent connection can't be closed   */
                                return false;   
                        }
                }               
                return $this->_connectionID;
        }
        
        /*      Returns: true on success, false on error 
                Select the database name to be used     */
        function selectDB($dbName) {
                $this->databaseName = $dbName;
                if ($this->_connectionID) {
                        return @sybase_select_db($dbName);              
                }
                else {
                        /*      No database selected    */
                        return false;
                }                       
        }

        /*      Returns: the Recordset object disregard success or failure
                Send the sql statement to the database server.  */
        function execute($sql = "") {
                $this->_queryID = @sybase_query($sql, $this->_connectionID);
                // Instantiate an object without considering whether
                // the query return any results or not
                $this->_tempResultObj = new Recordset($this->_queryID);
                $this->_insertQuery($this->_queryID);
                return $this->_tempResultObj;
        }

        /*      Returns: the last error message from previous database operation
                Note: This function is NOT available for Sybase.        */      

        function errorMsg() {
                $this->_errorMsg = "errorMsg() is not available for Sybase";
            return $this->_errorMsg;
        }

        /*      Returns: true on success, false on failure
                Close the database connection.  */      
                
        function close() {
                if ($this->_queryIDList && sizeof($this->_queryIDList > 0)) {
                        while(list($_key, $_resultID) = each($this->_queryIDList)) {
                                @sybase_free_result($_resultID);
                        }
                }
                // If its not a persistent connection, then 
                // only the connection needs to be closed
                if ($this->_isPersistentConnection != true) {   
                        return @sybase_close($this->_connectionID);
                }
                else {
                        return true;
                }
        }
        
        // A PRIVATE function used by the constructor function
        // of the query object.  insert the successful returned 
        // query id to the query id list.  Used for later results 
        // cleanup.  A private function that's never meant 
        // to be used directly
        function _insertQuery($query_id) {
                $this->_queryIDList[] = $query_id;
        }               
}
        
//----------------------------------
         Class Name: Recordset
//----------------------------------
class Recordset 
{
        /*      public variables        */
        var $fields;
        // indicates that the current record position is 
        // before the first record in a Recordset object
        var $BOF = null;        
        // indicates that the current record position 
        // is after the last record in a Recordset object
        var $EOF = null;
        
        // Private variables - starts with underscore
        var $_numOfRows = -1; // NEVER change the value! READ-ONLY!
        var $_numOfFields = -1; // NEVER change the value! READ-ONLY!
        // Holds anything that was returned from 
        // the database specific functions
        var $_tempResult = '';  
        // This variable keeps the result link identifier
        var $_queryID = -1;     
        // This variable keeps the current row in the Recordset
        var $_currentRow = -1;  

        // Returns: query id on success and false if 
        // failed Constructor function
        function Recordset($queryID) {
                $this->_queryID = $queryID;
                if ($queryID) {
                        $this->_numOfRows = @sybase_num_rows($this->_queryID);
                        $this->_numOfFields = @sybase_num_fields($this->_queryID);
                }
                else {
                        $this->_numOfRows = 0;
                        $this->_numOfFields = 0;
                }
                /*      If result set contains rows     */
                if ($this->_numOfRows > 0 && $this->_currentRow == -1) {
                        $this->_currentRow = 0;
                        $this->fields = @sybase_fetch_array($this->_queryID);
                        $this->EOF = false;
                        $this->BOF = false;
                }
                return $this->_queryID;
        }
                
        // Returns: true if successful, false if fail Set 
        // the Recordset pointer to a specified field offset. 
        // If the next call to fetchField() won't include a 
        // field offset, this field would be returned
        function fieldSeek($fieldOffset = -1) {
                $this->_tempResult = @sybase_field_seek($this->_queryID, $fieldOffset);
                return $this->_tempResult;
        }

        // Returns: an object containing field information.  
        // Get column information in the Recordset object. 
        // fetchField() can be used in order to obtain information
        // about fields in a certain query result. If the field 
        // offset isn't specified, the next field that wasn't yet
        // retrieved by fetchField() is retrieved
        function fetchField($fieldOffset = -1) {
                if ($fieldOffset != -1) {
                        $this->_tempResult = @sybase_fetch_field($this->_queryID, $fieldOffset);
                }
                /*      The $fieldOffset argument is not provided thus its -1   */
                else if ($fieldOffset == -1) {  
                        $this->_tempResult = @sybase_fetch_field($this->_queryID);
                }
                return $this->_tempResult;
        }

        // Returns: true if there still rows available, or 
        // false if there are no more rows.  Moves to the next
        // row in a specified Recordset object and makes that record 
        // the current row and the data corresponding to the row will
        // be retrieved into the fields collection.  Note: Unlike 
        // the moveRow() method, when _currentRow is getNumOfRows() - 1,
        // EOF will immediately be true.  If row number is not 
        // provided, the function will point to the 
        // first row automatically
        function nextRow() {
                if ($this->getNumOfRows() > 0) {                        
                        $this->fields = array();
                        $this->_currentRow++;
                        $this->fields = @sybase_fetch_array($this->_queryID);
                        /*      This is not working.  True all the time */
                        if ($this->fields) {
                                $this->_checkAndChangeEOF($this->_currentRow - 1);
                                return true;
                        }
                }
                $this->EOF = true;
                return false;
        }       
                        
        // Returns: true on success, false on failure moveRow()
        // moves the internal row pointer of the Recordset object 
        // to point to the specified row number and the data 
        // corresponding to the row will be retrieved into the
        // fields collection.  If row number is not provided, the
        // function will point to the first row automatically
        function moveRow($rowNumber = 0) {
                if ($rowNumber == 0) {
                        return $this->firstRow();
                }
                else if ($rowNumber == ($this->getNumOfRows() - 1)) {
                        return $this->lastRow();
                }
                if ($this->getNumOfRows() > 0 && $rowNumber < $this->getNumOfRows()) {       
                        $this->fields = null;
                        $this->_currentRow = $rowNumber;
                        if(@sybase_data_seek($this->_queryID, $this->_currentRow)) {
                                $this->fields = @sybase_fetch_array($this->_queryID);
                                /*      This is not working.  True all the time */
                                if ($this->fields) {    
                                        // No need to call _checkAndChangeEOF() 
                                        // because the possibility of moving to the 
                                        // last row has been handled by the above code
                                        $this->EOF = false; 
                                        return true;
                                }
                        }
                }
                $this->EOF = true;
                return false;
        }

        // Returns: true on success, false on failure firstRow()
        // moves the internal row pointer of the Recordset object
        // to the first row and the data corresponding to the row 
        // will be retrieved into the fields collection
        function firstRow() {
                if ($this->getNumOfRows() > 0) {
                        $this->fields = array();
                        $this->_currentRow = 0;
                        if (@sybase_data_seek($this->_queryID, $this->_currentRow)) {
                                $this->fields = @sybase_fetch_array($this->_queryID);
                                $this->EOF = false;
                                /*      This is not working.  True all the time */
                                if ($this->fields) {
                                        return true;    
                                }
                        }
                }
                $this->EOF = true;
                return false;           
        }

        // Returns: true on success, false on failure lastRow() 
        // moves the internal row pointer of the Recordset object
        // to the last row and the data corresponding to the row
        // will be retrieved into the fields collection
        function lastRow() {
                if ($this->getNumOfRows() > 0) {        
                        $this->fields = array();        
                        $num_of_rows = $this->getNumOfRows();
                        $this->_tempResult = @sybase_data_seek($this->_queryID, --$num_of_rows);
                        if ($this->_tempResult) {
                                /*      $num_of_rows decemented at above        */
                                $this->_currentRow = $num_of_rows;
                                $this->fields = @sybase_fetch_array($this->_queryID);
                                /*      This is not working.  True all the time */
                                if ($this->fields) {
                                        /*      Special case for making EOF false.      */
                                        $this->EOF = false;
                                        return true;
                                }
                        }
                }
                $this->EOF = true;
                return false;
        }

        // close() only needs to be called if you are worried
        // about using too much memory while your script is 
        // running. All associated result memory for the 
        // specified result identifier will automatically be freed
        function close() {
                $this->_tempResult = @sybase_free_result($this->_queryID);              
                return $this->_tempResult;
        }

        /*      Returns: the number of rows in a result set. 
        Get number of rows in result.   */
        
        function getNumOfRows() {
                return $this->_numOfRows;
        }

        /*      Returns: the number of fields in a result set. 
        Get number of fields in result. */
                
        function getNumOfFields() {
                return $this->_numOfFields;
        }       

        /*      Check and change the status of EOF.     */              
        function _checkAndChangeEOF($currentRow) {
                if ($currentRow >= ($this->_numOfRows - 1)) {
                        $this->EOF = true;
                }               
                else {
                        $this->EOF = false;             
                }
        }
}
?>

19. ºÎ·Ï I phpDB.inc ¿¹Á¦

Á¦ÃâÀÚ : Joe Thong darkjoe@softhome.net Site URL: http://phpdb.linuxbox.com

Description: ¿©·¯°¡Áö µ¥ÀÌÅͺ£À̽º ¼­¹ö¿¡¼­ °á°ú Á¶ÀÛÀ» À§ÇÑ °­·ÂÇÑ PHP µ¥ÀÌŸº£À̽º ·¦ÆÛ. µ¥ÀÌÅͺ£À̽ºÀÇ °á°ú°¡ phpDB¿¡ ÀÇÇØ ÀÚµ¿À¸·Î ³ª¿À°Ô µÈ´Ù.

ÀÌÈ­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÁ®¿¡¼­ '´Ù¸¥À̸§À¸·Î ÀúÀå'À» ´©¸£°í phpDB.inc ¶ó°í ÀÔ·ÂÇϽÿÀ.


<?php
/*
Name: phpDB General module
Version: 1.02bR6
Description: A PHP database wrapper for various 
        database servers.  Database results are flushed 
        automatically by phpDB.  Supported database 
        servers are MySQL, MSQL, PostgreSQL, Microsoft 
        SQL Server and Sybase. 
*/

        if (!defined("_PHPDB_GENERAL_LAYER")) {
          define("_PHPDB_GENERAL_LAYER", 1 );
        }
        else
                return;
        
        // Fill in the database server that you're
        // going to use.  Consult the phpDB Reference
        // Manual for more information
        $databaseType = '';
        // The phpDB module root path.  No trailing slash
        $phpDBRootPath = '.';   
        
        function useDB($dbType = "") 
        {
                GLOBAL $phpDBRootPath;
                switch (strtolower($dbType)) 
                {
                        case "mysql":
                        case "msql":
                        case "postgresql":
                        case "mssql":
                        case "sybase":
                        case "informix":
                                include("$phpDBRootPath". "/phpDB-" . "$dbType.lib");
                                break;
                        case "":
                                die("Please edit phpDB.inc in order to use phpDB");
                                return false;
                        default:
                                die("Invalid database selection");
                                return false;           
                }                                       
                return true;            
        }
        
        useDB($databaseType);
?>

20. ºÎ·Ï J phpDBTest.php3 ¿¹Á¦

Á¦ÃâÀÚ : Joe Thong darkjoe@softhome.net Site URL: http://phpdb.linuxbox.com

¼³¸í: ¿©·¯°¡Áö µ¥ÀÌÅͺ£À̽º ¼­¹ö¿¡¼­ °á°ú Á¶ÀÛÀ» À§ÇÑ °­·ÂÇÑ PHP µ¥ÀÌŸº£À̽º ·¦ÆÛ. µ¥ÀÌÅͺ£À̽ºÀÇ °á°ú°¡ phpDB¿¡ ÀÇÇØ ÀÚµ¿À¸·Î ³ª¿À°Ô µÈ´Ù.

ÀÌÈ­ÀÏÀ» ¾òÀ¸·Á¸é À¥ºê¶ó¿ìÁ®¿¡¼­ '´Ù¸¥À̸§À¸·Î ÀúÀå'À» ´©¸£°í phpDB-mssql.lib ¶ó°í ÀÔ·ÂÇϽÿÀ.(¿ªÁÖ: phpDB-mssql.lib °¡ ¾Æ´Ï°í. phpDBTest.php3 ¶ó°íÇؾßÇÕ´Ï´Ù.)


<html>
< head>
        < title>Untitled< /title>
< /head>

< body>
<?php
        // ÀÌÈ­ÀÏÀº phpDB.inc°¡ ÀÖ´Â µð·ºÅ丮¿¡ ÀÖ´Ù°í °¡Á¤ÇÕ´Ï´Ù.
        include("phpDB.inc");   
        $db = new phpDB();
        $db->pconnect("hostName", "userName", "passWord", "databaseName") or die ("Can't connect to database server or select database");
        $rs = $db->execute("SELECT * FROM Items");
        $numOfRows = $rs->getNumOfRows();
        echo "Number of Rows: $numOfRows";      

        $rs->firstRow(); // ¼±ÅûçÇ×À̳ª, ÃßõÇÕ´Ï´Ù.
        while (!$rs->EOF) {
                // Fields collection accessible as associative arrays too
                echo "<br>" . $rs->fields[0];
                $rs->nextRow(); // NOTE: nextRow() is placed at below
        }

        $rs->close();           
        $db->close();   // ¼±ÅûçÇ×
?>
< /body>
< /html>


ID
Password
Join
It is Fortune, not wisdom that rules man's life.


sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2009-04-17 15:49:35
Processing time 0.0016 sec