<?php
namespace app\ common\ tool ;
class PhpOci {
private $conn = null ;
private $stmt = null ;
private static $instance ;
private $message = null ;
private $affectedRows = 0 ;
public function __construct ( )
{
$this - > connect ( ) ;
}
public function getAffectRows ( ) {
return oci_num_rows ( $this - > stmt ) ;
}
public function getMessage ( ) {
return $this - > message ;
}
public function connect ( ) {
$this - > conn = oci_connect ( 'scott' , 'scott' , '10.87.230.200/orcl' , 'UTF8' ) ;
if ( ! $this - > conn ) {
$this - > message = oci_error ( ) ;
}
return $this - > conn ;
}
public static function getInstance ( ) {
if ( self: : $instance == false ) {
self: : $instance = new self ;
}
return self: : $instance ;
}
function create_table ( $sql )
{
$this - > stmt = oci_parse ( $this - > conn , $sql ) ;
oci_execute ( $this - > stmt ) ;
return true ;
}
function drop_table ( $sql )
{
$this - > stmt = oci_parse ( $this - > conn , $sql ) ;
oci_execute ( $this - > stmt ) ;
return true ;
}
function insert_data ( $sql )
{
$this - > stmt = oci_parse ( $this - > conn , $sql ) ;
oci_execute ( $this - > stmt , OCI_DEFAULT ) ;
echo $this - > conn . " inserted hallo\n\n" ;
}
function commit ( )
{
oci_commit ( $this - > conn ) ;
}
function rollback ( )
{
oci_rollback ( $this - > conn ) ;
}
public function findAll ( $sql )
{
$data = [ ] ;
$this - > stmt = oci_parse ( $this - > conn , $sql ) ;
oci_execute ( $this - > stmt , OCI_DEFAULT ) ;
while ( ( $row = oci_fetch_assoc ( $this - > stmt ) ) != false ) {
$data [ ] = $row ;
}
return $data ;
}
public function findOne ( $sql )
{
$data = [ ] ;
$this - > stmt = oci_parse ( $this - > conn , $sql ) ;
oci_execute ( $this - > stmt , OCI_DEFAULT ) ;
while ( ( $row = oci_fetch_assoc ( $this - > stmt ) ) != false ) {
$data [ ] = $row ;
}
if ( ! empty ( $data ) )
{
$data = $data [ 0 ] ;
}
return $data ;
}
public function autoInsert ( $data , $table ) {
$sql = "insert into " . $table . " " ;
$fields = array_keys ( $data ) ;
$fields = array_map ( function ( $v ) { return $v ; } , $fields ) ;
$fields_str = implode ( ', ' , $fields ) ;
$sql . = '(' . $fields_str . ')' ;
$values = array_map ( function ( $v ) { return "'" . $v . "'" ; } , $data ) ;
$values_str = implode ( ', ' , $values ) ;
$sql . = ' values (' . $values_str . ')' ;
try {
$this - > stmt = oci_parse ( $this - > conn , $sql ) ;
oci_execute ( $this - > stmt , OCI_DEFAULT ) ;
$this - > commit ( ) ;
return true ;
} catch ( \Exception $e ) {
$this - > rollback ( ) ;
return false ;
}
}
public function autoUpdate ( $table , $data , $id = 'id' ) {
$str = "" ;
$pk_value = '' ;
if ( ! empty ( $data ) ) {
if ( isset ( $data [ $id ] ) ) {
$pk_value = $data [ $id ] ;
unset ( $data [ $id ] ) ;
}
foreach ( $data as $k = > $v ) {
$str . = "$k =" . "'" . $v . "'" . "," ;
}
$str = substr ( $str , 0 , - 1 ) ;
}
$sql = "update { $table } set " . $str . " where { $id } ='" . $pk_value . "'" ;
try {
$this - > stmt = oci_parse ( $this - > conn , $sql ) ;
oci_execute ( $this - > stmt , OCI_DEFAULT ) ;
$this - > commit ( ) ;
return true ;
} catch ( \Exception $e ) {
$this - > rollback ( ) ;
return false ;
}
}
public function __destruct ( ) {
oci_free_statement ( $this - > stmt ) ;
oci_close ( $this - > conn ) ;
}
}
使用方法
<?php
include ( 'PhpOci.php' ) ;
$db = PhpOci: : getInstance ( ) ;
$row = $db - > findOne ( "select A.* from users A where id=10" ) ;
var_dump ( $row ) ;