PHPでDB接続

 


(8.0.31)

https://weblabo.oscasierra.net/centos7-php74-install/
https://devsakaso.com/php-connect-mysql/


OS: CentOS7


-- 1. phpインストール

yum -y install epel-release
yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
ls -l /etc/yum.repos.d/

yum repolist all

yum search php74
yum -y install php74 php74-php php74-php-pecl-mysql

echo "<?php phpinfo(); ?>" | php74

php74 -i | grep PDO

 

-- 2. DB接続確認

vim a.php

<?php
$user = 'root';
$pwd = 'password';
$host = 'localhost';
$port = 3306;
$dbname = 'test';

$dsn = "mysql:host=$host;port=$port;dbname=$dbname;";
$conn = new PDO($dsn, $user, $pwd);

$pst = $conn->query('select * from tab1');
$result = $pst->fetchAll(PDO::FETCH_ASSOC);

print_r($result);

$conn = null;
?>


cat a.php | php74


 

(19c)
https://engineeringnote.hateblo.jp/entry/database/oracle/oci8-install
https://pecl.php.net/package/oci8
https://mike-neko.github.io/blog/oracle-cent/
https://lets.postgresql.jp/documents/technical/systemtap/1


OS: CentOS7

-- 1. phpインストール

yum -y install epel-release
yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

yum repolist all

yum search php74
yum -y install php74 php74-php php74-php-pear php74-php-devel php74-php-cli

※↓ エラー 「Cannot find sys/sdt.h which is required for DTrace support」回避のため追加インストール
yum -y install systemtap systemtap-runtime systemtap-sdt-devel


echo "<?php phpinfo(); ?>" | php74


-- 2. Oracle Instant Clientインストール

yum -y install https://download.oracle.com/otn_software/linux/instantclient/1917000/oracle-instantclient19.17-basic-19.17.0.0.0-1.x86_64.rpm

yum -y install https://download.oracle.com/otn_software/linux/instantclient/1917000/oracle-instantclient19.17-devel-19.17.0.0.0-1.x86_64.rpm

 


-- 3. OCI8インストール

wget https://pecl.php.net/get/oci8-2.2.0.tgz

echo "extension=oci8.so" > /etc/opt/remi/php74/php.d/90_oci8.ini

export PHP_DTRACE=yes

C_INCLUDE_PATH=/usr/lib/oracle/19.17/client64 /opt/remi/php74/root/usr/bin/pecl install oci8-2.2.0.tgz

 

php74 -i | grep OCI

 

-- 4. DB接続確認

vim a.php

<?php
    $conn = oci_connect('test', 'test', 'localhost:1521/pdb1.example.com');
    if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }
    $stid = oci_parse($conn, 'SELECT * FROM TAB1');
    oci_execute($stid);
    echo "COL1,COL2\n";
    while (oci_fetch($stid) ) {
        echo oci_result($stid, 'COL1') . ",";
        echo oci_result($stid, 'COL2') . "\n";
    }
    oci_close($conn);
?>

cat a.php | php74

 

(15)

https://blog.pinkumohikan.com/entry/how-to-install-php74-to-centos8
https://qiita.com/ga_ku/items/01191cc2fecf4a4313d2

 

OS: Rocky Linux 8

-- 1. phpインストール

dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm
dnf repolist all

dnf search php74
dnf -y install php74 php74-php php74-php-pgsql

echo "<?php phpinfo(); ?>" | php74

php74 -i | grep PDO


-- 2. DB接続確認

vim a.php

<?php
$user = 'postgres';
$pwd = 'postgres';
$host = '127.0.0.1';
$port = 5432;
$dbname = 'test';

$dsn = "pgsql:host=$host;port=$port;dbname=$dbname;";
$conn = new PDO($dsn, $user, $pwd);

$pst = $conn->query('select * from tab1');
$result = $pst->fetchAll(PDO::FETCH_ASSOC);

print_r($result);

$conn = null;
?>

 

cat a.php | php74

 

 

(2019)

https://blog.maro.style/post-46/


OS: CentOS7


-- 1. phpインストール

yum -y install epel-release
yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

yum repolist all

yum search php74
yum -y install php74 php74-php php74-php-sqlsrv

echo "<?php phpinfo(); ?>" | php74

php74 -i | grep PDO


-- 2. DB接続確認

vim a.php

<?php
$user = 'SA';
$pwd = 'password';
$host = 'localhost';
$port = 1433;
$dbname = 'test';

$dsn = "sqlsrv:server=$host,$port;database=$dbname";
$conn = new PDO($dsn, $user, $pwd);

$pst = $conn->query('select * from tab1');
$result = $pst->fetchAll(PDO::FETCH_ASSOC);

print_r($result);

$conn = null;
?>


cat a.php | php74