Nov 7

开始自学php,代码是自己从网上找的,写在这里作为笔记翻看。

我有SSH整合的经验,也有基础的jdbc知识,学php照猫画虎吧。

查询功能:

 

<?php
//设置页面编码格式
header ( "content-Type: text/html; charset=GBK" );
//生成连接对象$conn,三个参数分别是数据库的用户名,密码,以及sid
$conn = oci_connect ( 'testphp', '123', 'ORACLE11' );
//sql语句
$sql = "select * from userinfo";
//这句话相当于jdbc的preparestatement对象
$statement = oci_parse ( $conn, $sql );
//执行,返回值类型为boolean
if (oci_execute ( $statement )) {
//OCI_BOTH是数字和列名同时索引,如$row[1]和$row['username']的意义是相同的
	while ( ($row = oci_fetch_array ( $statement, OCI_BOTH )) ) {
		echo $row ['USERNAME'] . " " . $row ['PASSWORD'] . "<br>";
		
	}
}
//释放以及关闭资源
oci_free_statement ( $statement );
oci_close ( $conn );
?> 

插入功能:

 

<?php
header ( "content-Type: text/html; charset=GBK" );
?>
<script type="text/javascript">
function validate(){
	var username=document.getElementById("username").value;
	var password=document.getElementById("password").value;
	var pwd=document.getElementById("pwd").value;

	
		document.getElementById("form1").submit();
	
	
	
}
</script>
<form id="form1" action="insertUserinfo.php" method="post">
<table>
	<tr>
		<td>用户名</td>
		<td><input type="text" id="username" name="username"></td>
	</tr>
	<tr>
		<td>密码</td>
		<td><input type="password" id="password" name="password"></td>
	</tr>
	<tr>
		<td>确认密码</td>
		<td><input type="password" id="pwd" name="pwd"></td>
	</tr>
	<tr>
		<td><input type="button" onclick="validate()" id="ok" value="提交"></td>
		<td></td>
	</tr>
</table>
</form>

后台处理:

 

<?php
header ( "content-Type: text/html; charset=GBK" );

$username = $_POST ["username"];
$password = $_POST ["password"];
//mb_convert_encoding($username,"utf-8");

$conn = oci_connect ( 'testphp', '123', 'ORACLE11' );

$statement = oci_parse ( $conn, "insert into userinfo values(idauto.nextval,:username,:password) RETURNING ROWID INTO :id" );

$rowid = oci_new_descriptor ( $conn, OCI_D_ROWID );

oci_bind_by_name ( $statement, ":username", $username, SQL_VARCHAR );
oci_bind_by_name ( $statement, ":password", $password, SQL_VARCHAR );
oci_bind_by_name ( $statement, ":id", $rowid, - 1, OCI_B_ROWID );
if(oci_execute ( $statement)){
	echo $username." ".$password;
}
oci_commit ( $conn );
$rowid->free ();
oci_free_statement ( $statement );
oci_close ( $conn );
?>

这个insert我看了N久的代码,oci上面写的还比较完整,网友也给予一些提示。

为什么页面是GBK格式?因为我插入Oracle的话,utf-8是乱码,GBK就没事,这个问题先这样解决,以后想想如何在utf-8的页面格式下插入数据库

insert语句插入数据库时传参方式和hibernate有点相似,也是冒号:设置参数,不同的是处理sequence上,hibernate在配置文档中写好,php写在sql语句中,估计用到框架的话,也不用再写idauto.nextval了,id是sequence的值,需要我们用oci_new_descriptor 规定一个新的描述,oci_bind_by_name 相当于hibernate的setQuery语句进行传参复制,我甚至怀疑php天生就是防注入的。好了就是这样,update和delete再准备做实验