注册
PHP连接达梦数据库
专栏/培训园地/ 文章详情 /

PHP连接达梦数据库

wqf 2024/03/27 628 0 0
摘要

1 安装PHP
1.1 下载PHP安装包
[root@slave2 dmsetup]# wget https://www.php.net/distributions/php-7.4.2.tar.gz

1.2 解压安装包
[root@slave2 dmsetup]# tar -zxvf php-7.4.2.tar.gz

1.3 安装PHP依赖包
[root@slave2 dmsetup]# yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
[root@slave2 php-7.4.2]# yum install sqlite-devel

指定PHP安装路径
[root@slave2 php-7.4.2]# ./configure --prefix=/opt/php/ --with-config-file-path=/opt/php
image.png

编译安装PHP,耗时较长
[root@slave2 php-7.4.2]# make
[root@slave2 php-7.4.2]# make install
设置软连接
[root@slave2 bin]# ln -s /opt/php/bin/php /usr/bin/php
创建php.ini
[root@slave2 php-7.4.2]# cp -r php.ini-production /opt/php/php.ini
检查php版本(注意PHP是NTS【非线程安全】的,不提供数据访问保护)
[root@slave2 php]# php -v
PHP 7.4.2 (cli) (built: Dec 8 2022 17:59:44) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

2 配置环境变量
[root@slave2 php]# vi /etc/profile
export DM_HOME=/opt/dmdbms
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/dmdbms/bin:/opt/dmdbms/drivers/php_pdo
[root@slave2 php]# source /etc/profile

3 配置php.ini
[root@slave2 php]# vi php.ini
#添加如下内容
[PHP_DM]
extension_dir="/opt/dmdbms/drivers/php_pdo"
extension=libphp74_dm.so

[dm]
dm.port=5236
dm.allow_persistent = 1
dm.max_persistent = -1
dm.max_links = -1
dm.default_host = 192.168.18.130
dm.default_db = DAMENG
dm.default_user = SYSDBA
dm.default_user = SYSDBA
dm.connect_timeout = 10
dm.defaultlrl = 4096
dm.defaultbinmode = 1
dm.check_persistent = ON
验证驱动加载情况
[root@slave2 php]# php -m|grep DM
DM

4 测试数据库操作
4.1 连接数据库

[root@slave2 php]# cat php_conn.php
<?php
   header("Content-type:text/html;charset=utf-8"); //防止页面乱码
try
{
    $link = dm_connect("192.168.18.130:5236", "SYSDBA", "SYSDBA")
        or die("Could not connect : " . dm_error()."\n");  

    //使用 dm_error 会显示 dm 的 php 接口返回的错误,执行成功,则继续往下执行。
    print "php: Connected successfully"."\n"; 

    /*断开连接*/ 
    dm_close($link); 
}
catch(Exception $e)
{
    $e->getMessage() . "<br/>";
}
?>

测试PHP
[root@slave2 php]# php php_conn.php
php: Connected successfully

4.2 数据库DML操作

[root@slave2 php]# cat php_dml.php
<?php
   header("Content-type:text/html;charset=utf-8"); //防止页面乱码
try
{
    $link = dm_connect("192.168.18.130:5236", "SYSDBA", "SYSDBA")
        or die("Could not connect : " . dm_error()."\n");  

    //使用 dm_error 会显示 dm 的 php 接口返回的错误,执行成功,则继续往下执行。
    print "php: Connected successfully"."\n"; 
    //清空表,初始化测试环境
    $result = dm_exec($link, 'delete from TEST.COURSE') or die("Query failed : " . dm_error()."\n");  


    //插入数据
    $result = dm_exec($link, "insert into TEST.COURSE(NAME) values('语文'), ('数学'), ('英语'), ('体育')") 
        or die("Query failed : " . dm_error()."\n");  
    print "php: insert success"."\n"; 

    //删除数据
    $result = dm_exec($link, "delete from TEST.COURSE where name='数学'") or die("Query failed : " . dm_error()."\n");  
    print "php: delete success"."\n"; 

    //更新数据
    $result = dm_exec($link, 'update TEST.COURSE set name = \'英语-新课标\' where name=\'英语\'') or die("Query failed : " . dm_error()."\n");  
    print "php: update success"."\n"; 

    //查询数据
    $result = dm_exec($link, "select * from TEST.COURSE") or die("Query failed : " . dm_error()."\n");  
    print "<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n"; 
    while ($line = dm_fetch_array($result)) 
    { 
    print "\t<tr>\n"; 
    foreach ($line as $col_value) { 
            print "\t\t<td>$col_value</td>\n"; 
        } 
            print "\t</tr>\n"; 

    } 
    print "</table>\n";    

    /*释放资源*/ 
    dm_free_result($result); 
    print "php: select success"."\n"; 


    /*断开连接*/ 
    dm_close($link); 
}
catch(Exception $e)
{
    $e->getMessage() . "<br/>";
}
?>

测试PHP
[root@slave2 php]# php php_dml.php
image.png
4.3 数据库循环写入

[root@slave2 php]# vi php_demo.php
<?php
   header("Content-type:text/html;charset=utf-8"); //防止页面乱码
try
{
    $link = dm_connect("192.168.18.130:5236", "SYSDBA", "SYSDBA")
        or die("Could not connect : " . dm_error()."\n");  

    //使用 dm_error 会显示 dm 的 php 接口返回的错误,执行成功,则继续往下执行。
    print "php: Connected successfully"."\n"; 
	
	while(true) {
        print "query: select 1 from dual... ";
		
		$result = dm_exec($link, "select 1 from dual") or die("Query failed : " . dm_error()."\n"); 
		

        print "response:" . $result . "\n";
        sleep(5);
    }

    /*释放资源*/ 
    dm_free_result($result); 
    print "php: select success"."\n"; 
    /*断开连接*/ 
    dm_close($link); 
}
catch(Exception $e)
{
    $e->getMessage() . "<br/>";
}
?>

测试PHP
[root@slave2 php]# php php_demo.php
image.png

评论
后发表回复

作者

文章

阅读量

获赞

扫一扫
联系客服