Home | Mirror | Search

4. Database Extensions

4.1. sqlite

		
mkdir /srv/php-5.2.17/etc/conf.d
/srv/php-5.2.17/bin/phpize
./configure --with-php-config=/srv/php-5.2.17/bin/php-config
make && make install
		
		
		
cat > /srv/php-5.2.17/etc/conf.d/sqlite.ini <<EOF
extension=sqlite.so
extension=pdo_sqlite.so
EOF
		
		

4.2. mysqli

		
mkdir /srv/php-5.2.17/etc/conf.d
/srv/php-5.2.17/bin/phpize
./configure --with-php-config=/srv/php-5.2.17/bin/php-config
make && make install
		
		
		
cat > /srv/php-5.2.17/etc/conf.d/mysqli.ini <<EOF
extension=mysqli.so
EOF
		
		

4.3. MongoDB

sudo pecl install mongo
		
vim /srv/php/etc/conf.d/mongo.ini
extension=mongo.so
		

4.4. php-redis.x86_64 : Extension for communicating with the Redis key-value store

https://github.com/nicolasff/phpredis

yum search redis

php-redis.x86_64 : Extension for communicating with the Redis key-value store
python-redis.noarch : A Python client for redis
redis.x86_64 : A persistent key-value database
		

安裝git版本控制客戶端

yum install git
		

從github倉庫中克隆一份代碼到本地

git clone git://github.com/nicolasff/phpredis.git
		

編譯安裝phpredis; 我暫時沒有找到 pecl的phpredis源

		
cd phpredis
phpize
./configure --with-php-config=/srv/php-5.4.9/bin/php-config
make && make install
		
		

創建配置檔案

		
cat > /srv/php-5.4.9/etc/conf.d/redis.ini <<EOF
extension=redis.so
EOF
		
		

查看安裝情況

# php -m | grep redis
redis
		
		
session.save_handler = redis
session.save_path = "tcp://127.0.0.1:6379"
或者使用多個redis
session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2"
		
		

4.5. php5-pgsql - PostgreSQL module for php5

$ sudo apt-get install php5-pgsql
		

4.6. PHP connect SQL Server under unix like

Sql Server 支持由Freetds提供

主頁:http://www.freetds.org/

cd /usr/local/src/
wget ftp://ftp.ibiblio.org/pub/Linux/ALPHA/freetds/stable/freetds-stable.tgz
tar zxvf freetds-stable.tgz
cd freetds-0.64/

./configure --prefix=/usr/local/freetds-0.64
make
make install

ln -s /usr/local/freetds-0.64 /usr/local/freetds
		

configure

./configure --prefix=/usr/local/php-5.2.3 \
--with-config-file-path=/usr/local/php-5.2.3/etc \
--enable-fastcgi \
--enable-force-cgi-redirect \
--with-curl \
--with-gd \
--with-ldap \
--enable-zip \
--enable-exif \
--enable-pcntl \
--with-mssql=/usr/local/freetds

make
make test
make install
		

MSSQL在PHP中的配置如下

/usr/local/freetds/etc/freetds.conf

[MyServer2k]
       host = 10.10.10.11
       port = 3433
       tds version = 8.0
		

mssql.php 測試檔案

		
<?php
$conn = mssql_pconnect('MyServer2k', 'u_mobile', 'kEyt+_Zf.$P6');
mssql_select_db('D3_Mobile', $conn);
$query = mssql_query ('select * from dbo.MobileCommand where id=1');
$result = mssql_fetch_array ($query);
echo '<pre>';
print_r($result);
echo '</pre>';
?>
		
		

mssql_pconnect

resource mssql_connect ( [string servername [, string username [, string password [, bool new_link]]]] )

servername 指的是freetds.conf中定義伺服器名

測試

php -q mssql.php

4.7. MySQL

$ sudo apt-get install php5-mysql
		

4.8. oracle

pdo_oci/oci8

http://www.oracle.com/technetwork/topics/linuxx86-64soft-092277.html

export ORACLE_HOME=/usr/lib/oracle/11.1/client64/
export LD_LIBRARY_PATH=/usr/lib/oracle/11.1/client64:$LD_LIBRARY_PATH
export  NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
		

cd /usr/local/src/php-5.2.14/ext/pdo_oci
/usr/local/php-5.2.14/bin/phpize
./configure --with-php-config=/usr/local/php-5.2.14/bin/php-config --with-oci8=instantclient,/usr/lib/oracle/11.1/client64/lib
    make
    make install
		
   安裝後生成/usr/local/php/lib/php/extensions/no-debug-non-zts-yyyymmdd/oci8.so
   把oci8.so 移動到/usr/local/php/lib/php/extensions/目錄下
    mv /usr/local/php/lib/php/extensions/no-debug-non-zts-yyyymmdd/oci8.so ../

    extension_dir = /usr/local/php/lib/php/extensions/

   添加
     extension = pdo_oci.so
     extension = oci8.so
		

例 19.3. oracle

			
ln -s /usr/lib/oracle/10.2.0.3/client64 /usr/lib/oracle/10.2.0.3/client
ln -s /usr/include/oracle/10.2.0.3/client64    /usr/include/oracle/10.2.0.3/client

export ORACLE_HOME=/usr/lib/oracle/10.2.0.3/client64/
export LD_LIBRARY_PATH=/usr/lib/oracle/10.2.0.3/client64:$LD_LIBRARY_PATH
export  NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
# /usr/local/php-5.2.14/bin/phpize
# ./configure --with-php-config=/usr/local/php-5.2.14/bin/php-config  --with-pdo-oci=instantclient,/usr,10.2.0.3
# make && make install
			
			

php.ini

extension=pdo_oci.so
			

oci.php

			
    <?php
    $pdo= new PDO("oci:dbname=//localhost:1521/mydbname;charset=utf-8,username,password);

    $sql="select table_name as tname from user_tables";
    $query = $pdo->prepare($sql);
    $query->execute();

    for($i=0; $row = $query->fetch(); $i++){
      #print_r($row);
      echo $i." - ".$row[0]."<br/>";
      echo $i." - ".$row['TNAME']."<br/>";
    }
    ?>
			
			

comments powered by Disqus