PHPÐÂÊÖÉÏ·£¨Ê®¶þ£©

5年以前  |  阅读数:221 次  |  编程语言:PHP 

ʹÓÃPHPÀ´²Ù×÷OracleÊý¾Ý¿a

11. Êý¾Ý¿aÁ¬½Ó

¡¡¡¡ÔÚÉÏÒ»½ÚÀi£¬ÎÒÃÇÒѾ­½eÉÜÁËPHPÓeMySQLÊý¾Ý¿aµÄÒ»Ð(C)»u±¾²Ù×÷֪ʶ£¬ÔÚ»¥ÁªÍøÖÐÓйØPHPÓeMySQLµÄ½Ì³ÌÒ²×i¶a¡£MySQLÊÇÃaѵģ¬ÕaÒ»µaÒ²Ði¾ÍÎuÒýÁ˲»ÉÙÈË¡£ÓÉÓÚÆa¹aºÓ¦Óã¬ÎҾͲ»ÏeÔÚÕaÀi׸ÊoMySQLµÄʹÓý¨ÁË¡£Oracle±»´oÁ¿ÔÚÆoÒµÓ¦ÓÃÖвÉÓã¬Òo´ËÎÒÃǾÍÀuÓÃOracleÀ´½øÒ»²½½eÉÜPHPÓeÊý¾Ý¿aµÄÁ¬½Ó¡£ÎÒÃǵ±È»²»»aÌa¼°OracleÊý¾Ý¿aµÄÉe¼ÆÔ­Ài£¬Ô­ÒoÊÇÕaÒѾ­³¬³oÁËÎÒÃǵÄÌÖÂÛ*¶Î§¡£

¡¡¡¡PHPÌa¹(C)ÁËÁ½Ì׺¯ÊýÓeOracleÁ¬½Ó£¬Ö±ðÊÇORA_ºÍOCIº¯Êý¡£ÆaÖÐORA_º¯ÊýÂÔÏԳ¾ɡ£OCIº¯Êý¸uоÝ˵¸uºÃÒ»Ð(C)¡£Á½ÕßµÄʹÓÃÓi¨¼¸ºoÏa²iÎÞ¼¸¡£ÈçÇ°ËuÊo£¬ÄaµÄPHP°²×°Ñ¡ÏiÓ¦¸Ã¿ÉÒÔÖ§³ÖÁ½ÕßµÄʹÓá£
¡¡¡¡Ïe»ñµÃ¸u¶aÓйØÔÚMicrosoft Windowsƽ̨ÉÏ°²×°Ö§³ÖPHP3µÄApache*þÎñÆ÷µÄ֪ʶÒÔ¼°¸u¶aÓйØOracleÊý¾Ý¿aµÄ֪ʶ£¬Çe²eÔÄÒÔÏÂURL£ºwww.csoft.net/~vsbabu/articles/oraphp.html¡£

11.1 Á¬½Ó

<?
if ($conn=Ora_Logon("user@TNSNAME","password"))
{
echo "SUCCESS ! Connected to databasen";
}
else
{
echo "Failed :-( Could not connect to databasen";
}
Ora_Logoff($conn);
phpinfo();
?>
ÒÔÉÏ´uÂeʹÓÃTNSNAME£¨ÔÚÄaµÄtnsnames.oraÎļþÖÐÖ¸Ã÷£(C)¶¨ÒaµÄOracleÊý¾Ý¿aÃu³Æ¡¢Óû§Ãu³ÆºÍÃÜÂeÁ¬½ÓÊý¾Ý¿a¡£Ôڳɹ¦Á¬½ÓµÄ»u´¡ÉÏ£¬ora_logonº¯Êýµ»ØÒ»¸oÇÁaµÄÁ¬½ÓID²¢´¢´aeÔÚ±aÁ¿$connÖС£

11.2 ²eѯ

¼ÙÉeÓeÊý¾Ý¿aÒѾ­Á¬½Ó¾ÍÐ÷£¬ÏÂÃaeÎÒÃǾÍÀ´Êµ¼ÊµÄÓ¦ÓöÔÊý¾Ý¿aµÄ²eѯ¡£ÏÂÃaeµÄ´uÂeÑÝʾÁËÒ»¸oÁ¬½Ó²¢²eѯµÄµaÐÍÀý×Ó£º
<?
/*

  • Á¬½ÓÊý¾Ý¿a²¢Ö´Ðвeѯ
    /
    function printoraerr($in_cur)
    {
    // ¼i²eOracleÊÇ
    ñ³o´i
    // Èç¹u´aeÔÚ´iÎoÔoÏÔʾ
    // µ±Ö¸Õe±»¼¤»iʱÿ´ÎÇeÇoOracleºoµ÷Óøú¯Êý
    if(ora_errorcode($in_cur))
    echo "Oracle code - ".ora_error($in_cur)."n";
    return;
    }
    /* Ö÷³ÌÐo /
    if (!($conn=ora_logon("user@TNSNAME","password")))
    {
    echo "Connection to database failedn";
    exit;
    }
    echo "Connected as connection - $conn
    n";
    echo "Opening cursor ...
    n";
    $cursor=ora_open($conn); printoraerr($cursor);
    echo "Opened cursor - $cursor
    n";
    $qry="select user,sysdate from dual";
    echo "Parsing the query $qry ...
    n";
    ora_parse($cursor,$qry,0); printoraerr($cursor);
    echo "Query parsed
    n";
    echo "Executing cursor ...
    n";
    ora_exec($cursor); printoraerr($cursor);
    echo "Executed cursor
    n";
    echo "Fetching cursor ...
    n";
    while(ora_fetch($cursor))
    {
    $user=ora_getcolumn($cursor,0); printoraerr($cursor);
    $sysdate=ora_getcolumn($cursor,1); printoraerr($cursor);
    echo " row = $user, $sysdate
    n";
    }
    echo "Fetched all records
    n";
    echo "Closing cursor ...
    n";
    ora_close($cursor);
    echo "Closed cursor
    n";
    echo "Logging off from oracle...
    n";
    ora_logoff($conn);
    echo "Logged off from oracle
    n";
    ?>
    £¨ÒeÕß×¢£ºÒÔÉÏ´uÂe¶ÎȱÉÙ×¢ÊÍ£¬Çe¶ÁÕ߲ο¼PHP ManualµÄOracleÊý¾Ý¿aº¯Êý²¿*Ö£(C)

11.3 ÏÔʾ½a¹u

ÒÔÏ´uÂeÑÝʾÁËÔoÑu²eѯÊý¾Ý¿a²¢½«½a¹uÊa³o£º
<?
function printoraerr($in_cur, $conn)
{
// ¼i²eOracleÊÇ*ñ³o´i
// Èç¹u´aeÔÚ´iÎoÔoÏÔʾ
// µ±Ö¸Õe±»¼¤»iʱÿ´ÎÇeÇoOracleºoµ÷Óøú¯Êý
// If it encountered an error, we exit immediately
if(ora_errorcode($in_cur))
{
echo "Oracle code - ".ora_error($in_cur)."
n";
ora_logoff($conn);
exit;
}
return;
}

function exequery($w_qry,$conn)
{
$cursor=ora_open($conn); printoraerr($cursor,$conn);
ora_parse($cursor,$w_qry,0); printoraerr($cursor,$conn);
ora_exec($cursor); printoraerr($cursor,$conn);
$numrows=0;
$w_numcols=ora_numcols($cursor);
// ÏÔʾÍ*²¿
echo "

n"; for ($i=0;$i<$w_numcols;$i++) { $align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT"; echo "tn"; } echo "n"; while(ora_fetch($cursor)) { echo "n"; for ($i=0;$i<$w_numcols;$i++) { $align=(ora_columntype($cursor,$i)=="NUMBER")?"RIGHT":"LEFT"; if(ora_columntype($cursor,$i)=="LONG") echo "n"; else echo "n"; printoraerr($cursor,$conn); } $numrows++; echo "n"; } if ($numrows==0) echo "n"; else { echo "n"; echo "n"; echo "n"; echo "n"; } echo "
".ora_columnname($cursor,$i)."
".   
ora_getcolumn($cursor,$i)."
".ora_getcolumn($cursor,$i)."
Query returned no records
Count$numrows
n"; ora_close($cursor); return; }

// Ö÷³ÌÐo
if(!($conn=ora_logon("user@SID","password")))
{
echo "Error: Cannot connect to databasen";
exit;
}
$qry="SELECT
deptno "Dept"
,empno "Emp"
,empnm "Name"
,salary "Salary"
FROM
employee
ORDER BY 1,2";
exequery($qry);
ora_logoff($conn);
?>
£¨ÒeÕß×¢£ºÒÔÉÏ´uÂe¶ÎȱÉÙ×¢ÊÍ£¬Çe¶ÁÕ߲ο¼PHP ManualµÄOracleÊý¾Ý¿aº¯Êý²¿*Ö£(C)

11.4 »uÓÚHTTPµÄOracleµÇ¼

½«ÒÔÏ´uÂe¼ÓÔÚPHPÒ³Ãae´uÂe֮ǰÒÔÈÈÏOracleµÇ¼¡£×¢ÒaÄa±ØÐeÕýÈÉe¶¨$ SID¡£
<?
if(!isset($PHP_AUTH_USER))
{
Header("WWW-authenticate: basic realm="$SID"");
Header("HTTP/1.0 401 Unauthorized");
$title="Login Instructions";
echo "


You are not authorized to enter the site

n"; exit; } else { if (!($conn=ora_logon("$PHP_AUTH_USER@$SID",$PHP_AUTH_PW))) { Header("WWW-authenticate: basic realm="$SID""); Header("HTTP/1.0 401 Unauthorized"); $title="Login Instructions"; echo "
You are not authorised to enter the site
n"; exit; } } ?>
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例