温馨建议:
oracle数据库的表名称和字段名称用大写,不然你要注意下面这些问题,小写的表名称一定要用双引号括起来,字段名称要用双引号括起来,字段的值用单引号括起来。
想用php操作oracle,必须要在你的本机安装oracle哦!
user = $username;
$this->pass = $password;
$this->dsn = $dsn;
}
/**
* 连接数据库 written:dennyYang date:2019-12-12
*/
function conn(){
$newLink = NULL;
if(!$this->link){
try{
//$pdo = new PDO('oci:dbname=//IP:端口号/SID',用户名,密码);
$newLink = new PDO($this->dsn, $this->user,$this->pass);
$this->link = $newLink;
}catch(PDOException $e){
echo ($e->getMessage());exit();
}
}
if(!$this->link){
echo "oracle login error...";exit();
}
return $this->link;
}
/**
* 执行插入的sql语句 written:dennyYang date:2019-12-12
*/
function insertOne($paramOrSql=array(),$table_name='',$seq_name=''){
if(empty($paramOrSql)||empty($table_name)){
return 'Error ,empty param...';
}
$link = $this->conn();
//数组方式插入
if(is_array($paramOrSql)){
$field = $value='';
foreach($paramOrSql as $k=>$v){
$field .= "$k,";
$value .= "'$v',";
}
$field = trim($field,',');
$value = trim($value,',');
$paramOrSql = "INSERT INTO $table_name ($field) VALUES ($value)";
}
if($this->dev){
echo $paramOrSql."
";
}
$result = $link->exec($paramOrSql);
//获取插入的id
if($seq_name){
$result = $this->getLastInsertId($seq_name);
}
return $result;
}
/**
* 插入多条的sql语句 written:dennyYang date:2019-12-12
*/
function insertMulti($paramOrSql=array(),$table_name=''){
if(empty($paramOrSql)||empty($table_name)){
return 'Error ,empty param...';
}
$link = $this->conn();
//数组方式插入
if(is_array($paramOrSql)){
$sqlContent = 'INSERT ALL ';
foreach($paramOrSql as $k1=>$v1){
$field = $value='';
foreach ($v1 as $k2=>$v2){
$field .= "$k2,";
$value .= "'$v2',";
}
$field = trim($field,',');
$value = trim($value,',');
$sqlContent .= " INTO {$table_name} ({$field}) VALUES ({$value})".PHP_EOL;
}
$sqlContent .= " SELECT * from dual";
$paramOrSql = $sqlContent;
}
if($this->dev){
echo $paramOrSql."
";
}
$sth = $link->prepare($paramOrSql);
$sth->execute();
$result = $sth->rowCount();//获取插入条数
return $result;
}
/**
* 获取当前插入的id,前提是先有创建自增的序列,传入序列名称 written:dennyYang date:2019-12-12
*/
function getLastInsertId($seq_name="BOOKS_SEQ"){
$sql = "select $seq_name.CURRVAL from dual";
$link = $this->conn();
$sth = $link->prepare($sql);
$sth->execute();
$result = $sth->fetchAll();
return isset($result[0]['CURRVAL'])?$result[0]['CURRVAL']:'';
}
/**
* 查询数据集,返回二维数组 . written:dennyYang date:2019-12-12
*/
public function selectAll($sql=''){
if($this->dev){
echo $sql."
";
}
$link = $this->conn();
$sth = $link->prepare($sql);
$sth->execute();
$result = $sth->fetchAll();
if(empty($result)){
return false;
}
foreach($result as $k=>$v){
foreach($v as $k2=>$v2)
if(is_numeric($k2)){
unset($result[$k][$k2]);
}
}
return $result;
}
/**
* 获取sql的第一行数据 written:dennyYang date:2019-12-12
*/
public function selectOne($sql){
if($this->dev){
echo $sql."
";
}
$link = $this->conn();
$sth = $link->prepare($sql);
$sth->execute();
$result = $sth->fetch();
if(empty($result)){
return false;
}
foreach($result as $k=>$v){
if(is_numeric($k)){
unset($result[$k]);
}
}
return $result;
}
/**
* 更新数据 written:dennyYang date:2019-12-12
*/
public function update($sql=""){
if($this->dev){
echo $sql."
";
}
$link = $this->conn();
$sth = $link->prepare($sql);
$sth->execute();
$result = $sth->rowCount();
return $result;
}
/**
* 删除数据 written:dennyYang date:2019-12-12
*/
public function delete($sql=""){
if($this->dev){
echo $sql."
";
}
$link = $this->conn();
$sth = $link->prepare($sql);
$sth->execute();
$result = $sth->rowCount();
return $result;
}
/**
* 开启事务
*/
function start(){
$link = $this->conn();
return $link->beginTransaction();
}
/**
* 提交事务
*/
function commit(){
$link = $this->conn();
return $link->commit();
}
/**
* 回滚事务
*/
function rollback(){
$link = $this->conn();
return $link->rollBack();
}
}
表名,字段名必须用双引号括起来,字段非数值的必须用单引号,数值的可以不用单引号。
例子:books表
id是number类型,
name是varchar2类型
本例子php插入数据到oracle用的是
oracle 18c win 64位
php 7.2 版本 64位
温馨提示:在php 连接oracle的时候还要记住,php版本一定要跟oracle版本一样的,并且电脑上必须安装同样版本的oracle软件,不然oci8扩展是无法打开的哦!
1.INSERT INTO "books" ("id","name") values (10,"10bbb")
错 ORA-00984: 列在此处不允许 varchar2的字段必须用单引号
2.INSERT INTO 'books' ("id","name") values (10,'10bbb')
错 ORA-00903: 表名无效 books表必须用双引号括起来
3.INSERT INTO "books" (id,name) values (10,'10bbb')
错 ORA-00984: 列在此处不允许 列必须用双引号括起来
4.INSERT INTO "books" ('id','name') values (10,"10bbb")
错 ORA-00928: 缺失 SELECT 关键字 插入的列必须用双引号括起来,单引号不行,
并且name字段的值10bbb必须用单引号括起来,双引号不行。
5.INSERT INTO "books" ("id","name") values (10,'10bbb')
正确 表明用双引号,字段用双引号,字段name是varchar2用单引号括起来,id是number可以不用单引号
6.INSERT INTO "books" ("id","name") values ('11','11ccc')
正确 表明用双引号,字段用双引号,字段name是varchar2用单引号括起来,id是number可以不用单引号
如果当时创建表的时候表名称和字段名称都用大写的话就不用这么麻烦了:
直接 下面这样就行了
SELECT * FROM BOOKS
INSERT INTO BOOKS (NAME) VALUES (‘张三’)
所以小伙伴们用php插入数据到oracle的时候一定要严格遵守规则哦,表明用双引号,字段用双引号,字段的值用单引号!!!