Table of Contents
Foreword
结语
Home Backend Development PHP7 Detailed explanation of how to connect and use dm database in php7 (picture and text)

Detailed explanation of how to connect and use dm database in php7 (picture and text)

Feb 03, 2022 am 05:00 AM
php

Foreword


The company wants to engage in localized development. The database uses Dameng database. The Dameng version is dm8 and the PHP version is 7.2. The early stage is carried out on Windows. Develop.

The database is migrated from mysql to Dameng database. I will not go into the migration method and the PHP extension installation method. There is quite a lot of information on the Internet in this regard.

However, there is relatively little information on how to use PHP to connect to Dameng database. Everyone knows the customer service technology. All the information depends on reading documents. I will briefly provide you with the pitfalls I have stepped on and the methods. It is to directly operate the database, and then you can encapsulate it according to your own preferences.

Briefly explain Dameng's information directory. The "doc" under Dameng's installation directory mainly contains various usage documents. "drivers" contains extensions for various languages. PHP's so extension is Taken from here, I mainly use the extension of php_dm. There is too little information on pdo_dm, so I didn’t do much research.

Detailed explanation of how to connect and use dm database in php7 (picture and text)

Detailed explanation of how to connect and use dm database in php7 (picture and text)

There are two points that need to be reminded when instantiating the database. Character sets and string case sensitivity must be remembered in advance Set up well and don’t dig yourself into a hole.

My experience is that the data table names and field names migrated from MySQL are all lowercase, which results in various pitfalls when used later. Later I found that the case sensitivity can be removed.

Detailed explanation of how to connect and use dm database in php7 (picture and text)

Here we will use the CITY table in the official sample library to demonstrate various usage methods of php7.2.

Detailed explanation of how to connect and use dm database in php7 (picture and text)

Detailed explanation of how to connect and use dm database in php7 (picture and text)

##Query


Without further ado, let’s start with the code

//连接数据库
$link = dm_connect("localhost", "SYSDBA", "SYSDBA");
if(!$link){
    var_dump(@dm_error());
    var_dump(iconv("GBK","UTF-8",@dm_errormsg()));
}
dm_setoption($link,1,12345,1);//设置 dm 连接和语句的相关属性,设置UTF8

$query = "select * from DMHR.CITY";
$result = dm_exec($link,$query);

print " 查询结果:";
while ($line = dm_fetch_array($result)){
    print_r($line);
    echo '<br>';
}

/* 释放资源 */
dm_free_result($result);

/* 断开连接 */
dm_close($link);
Copy after login

The usage methods of Dameng's php official extension php5 and php7 are very different. Many methods in php5 cannot be used in php7. Some methods are replaced by other methods in php7. Most of the information on the Internet is php5. Yes, it is completely unusable in php7.

The problem of garbled Chinese characters in query statements is a sinkhole. I tried this method after checking the documentation for a long time. If there are other more elegant methods, please teach me, and everyone can learn from each other.

Insert

//连接数据库
$link = dm_connect("localhost", "SYSDBA", "SYSDBA");
if(!$link){
    var_dump(@dm_error());
    var_dump(iconv("GBK","UTF-8",@dm_errormsg()));
}
dm_setoption($link,1,12345,1);//设置 dm 连接和语句的相关属性,设置UTF8

$query = "INSERT INTO DMHR.CITY (CITY_ID,CITY_NAME,REGION_ID) VALUES ('JL','吉林','1')";
$result = dm_exec($link,$query);

if($result){
    echo "插入成功";
    //曲线查询插入id
    /*$query = "SELECT @@IDENTITY as insert_id";
    $result = dm_exec($link,$query);
    $line = dm_fetch_array($result);
    echo ',ID:';
    print_r($line);*/
}

/* 释放资源 */
dm_free_result($result);

/* 断开连接 */
dm_close($link);
Copy after login

The officially provided dm_insert_id() function seems to be only available in php5, php7 does not have this function, and can only be incremented through curve query The id value. Of course, the table in the demonstration does not have an auto-increment ID. At the same time, SELECT @@IDENTITY as insert_id must be successfully queried. Even if the insertion fails, the self-increment ID of the previous successful insertion will be returned. 千Never use the auto-increment ID to determine whether the statement is inserted successfully.

Update

//连接数据库
$link = dm_connect("localhost", "SYSDBA", "SYSDBA");
if(!$link){
    var_dump(@dm_error());
    var_dump(iconv("GBK","UTF-8",@dm_errormsg()));
}
dm_setoption($link,1,12345,1);//设置 dm 连接和语句的相关属性,设置UTF8

$query = "UPDATE  DMHR.CITY SET REGION_ID='2' WHERE CITY_ID='JL'";
$result = dm_exec($link,$query);

if($result){
    echo "更新成功";
}

/* 释放资源 */
dm_free_result($result);

/* 断开连接 */
dm_close($link);
Copy after login

Update is very simple

Delete

//连接数据库
$link = dm_connect("localhost", "SYSDBA", "SYSDBA");
if(!$link){
    var_dump(@dm_error());
    var_dump(iconv("GBK","UTF-8",@dm_errormsg()));
}
dm_setoption($link,1,12345,1);//设置 dm 连接和语句的相关属性,设置UTF8

$query = "DELETE FROM DMHR.CITY WHERE (CITY_ID='JL')";
$result = dm_exec($link,$query);

if($result){
    echo "删除成功";
}

/* 释放资源 */
dm_free_result($result);

/* 断开连接 */
dm_close($link);
Copy after login

Deletion is also very simple, there is nothing special to pay attention to

Transaction


According to the official documentation: "DM does not provide a statement that explicitly defines the start of a transaction. The first executable SQL statement (except the login statement) implicitly defines the start of the transaction. Start", this is the reason why there is no transaction start method defined, but when we want to start a transaction from a certain program, we can use the dm_autocommit() function to turn off the automatic commit of the transaction, and turn on the automatic commit after the program ends

//连接数据库
$link = dm_connect("localhost", "SYSDBA", "SYSDBA");
if(!$link){
    var_dump(@dm_error());
    var_dump(iconv("GBK","UTF-8",@dm_errormsg()));
}
dm_setoption($link,1,12345,1);//设置 dm 连接和语句的相关属性,设置UTF8

$query = "INSERT INTO DMHR.CITY (CITY_ID,CITY_NAME,REGION_ID) VALUES ('JL','吉林','1')";
$result = dm_exec($link,$query);

if($result){
    echo "插入成功。";
}

$result = dm_autocommit($link,false);//事务自动提交关闭
$query = "UPDATE  DMHR.CITY SET CITY_NAME='辽宁' WHERE (CITY_ID='SY')";
$result = dm_exec($link,$query);
if($result){
    echo "更新成功,回滚。";
}


dm_rollback($link);//回滚
//dm_commit($link);//提交

$result = dm_autocommit($link,true);//开启事务自动提交,结束事务

/* 断开连接 */
dm_close($link);
Copy after login
Pits that have been stepped on


1. Obtain the time in the timestamp format from the Dameng database


select DATEDIFF(s, '1970-01-01 00: 00:00', GETUTCDATE());

2. If the database used before is mysql, pay attention to the scale behind the two types of time formats DATETIME and TIMESTAMP, if it is not 0 The time accuracy will be longer


Detailed explanation of how to connect and use dm database in php7 (picture and text)

3. Some fields will become uppercase when queried, such as "count"


Detailed explanation of how to connect and use dm database in php7 (picture and text)

Solution: Change the fields Use double quotes to quote the above example:


select count(1) as "count" from "DMHR"."CITY";

四、group by语句的使用很严格(或者说mysql的group by过于放松),select中除聚合函数之外出现的所有字段必须要在group by里面。

比方举一个错误的例子:

select EMPLOYEE_NAME,JOB_ID from "DMHR"."EMPLOYEE" group by JOB_ID;
EMPLOYEE_NAME和字段没在group by 中,执行必定失败
提供一种解决思路:

select * from "DMHR"."EMPLOYEE" where EMPLOYEE_ID in (select min(EMPLOYEE_ID) as minid from "DMHR"."EMPLOYEE" group by JOB_ID)
Copy after login

同样select中如果有聚合函数之外的字段,需要加入group by。错误的例子:

select min(EMPLOYEE_ID),EMPLOYEE_NAME,JOB_ID from "DMHR"."EMPLOYEE";
select中有min()函数外还有其他字段,执行必定失败。
如果一定要在其他很多字段里面加入聚合函数,提供一种思路:

select t1.EMPLOYEE_NAME,t1.JOB_ID,t2.minid from "DMHR"."EMPLOYEE" t1
left join ( select min(EMPLOYEE_ID) as minid,JOB_ID from "DMHR"."EMPLOYEE" group by JOB_ID ) t2 on t2.JOB_ID=t1.JOB_ID
where t1.EMPLOYEE_ID in (select min(EMPLOYEE_ID) as minid from "DMHR"."EMPLOYEE" group by JOB_ID);
Copy after login

结语


目前踩过的坑就这些了,希望能够帮助到大家。
对于其他问题还是要多翻阅官方文档了。

The above is the detailed content of Detailed explanation of how to connect and use dm database in php7 (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles