var http_request = false;
function send_request(url,method) {//初始化、指定处理函数、发送请求的函数
http_request = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType(‘text/xml’);
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
http_request = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e) {}
}
}
if (!http_request) { // 异常,创建对象实例失败
window.alert(“不能创建XMLHttpRequest对象实例.”);
return false;
}
switch(method){
case 1: http_request.onreadystatechange = processRequest1;break;//选择操作函数
case 2: http_request.onreadystatechange = processRequest2;break;
case 3: http_request.onreadystatechange = processRequest3;break;
}
// 确定发送请求的方式和URL以及是否同步执行下段代码
http_request.open(“GET”, url, true);
http_request.send(null);
}
// 处理返回信息的函数
function processRequest1() {//操作函数1,调入省
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
document.getElementById(“statusTxt”).innerHTML=”";
addOptionGroup(“province”,http_request.responseText);
} else { //页面不正常
alert(“您所请求的页面有异常。”);
}
}else {//只要未读取完成
document.getElementById(“statusTxt”).innerHTML=”正则读取数据中……”;
}
}
function processRequest2() {//操作函数2,调入市
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
document.getElementById(“statusTxt”).innerHTML=”";
addOptionGroup(“city”,http_request.responseText);
} else { //页面不正常
alert(“您所请求的页面有异常。”);
}
}else {//只要未读取完成
document.getElementById(“statusTxt”).innerHTML=”正则读取数据中……”;
}
}
function processRequest3() {//操作函数3,输入省市
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
document.getElementById(“statusTxt”).innerHTML=”";
document.getElementById(“district”).value=http_request.responseText;
} else { //页面不正常
alert(“您所请求的页面有异常。”);
}
}else {//只要未读取完成
document.getElementById(“statusTxt”).innerHTML=”正则读取数据中……”;
}
}
function loadProvince() {//载入省
send_request(“select.server.php?action=province”,1);//服务端处理程序,操作函数
}
function loadCity() {//载入市
document.getElementById(“district”).value=”";//清空文本框
send_request(“select.server.php?action=city&id=”+document.getElementById(“province”).value,2);
}
function inputDistrict() {//向文本框输入选定的省市
send_request(“select.server.php?action=district&id=”+document.getElementById(“province”).value+”&id1=”+document.getElementById(“city”).value,3);
}
addOption.js
function addOption(objSelectNow,txt,val)
{
/// 使用W3C标准语法为SELECT添加Option
var objOption = document.createElement(“OPTION”);
objOption.text= txt;
objOption.value=val;
objSelectNow.options.add(objOption);
}
function addOptionGroup(selectId,optGroupString)
{
var optGroup = optGroupString.split(“,”);
var objSelect = document.getElementsByTagName(“SELECT”);
var objSelectNow = objSelect[selectId];
objSelectNow.length = 1;
/// 成组添加Options
for (i=1; i<optGroup.length; i++)
{
addOption(objSelectNow, optGroup[i], i);
}
}
<script type=”text/javascript”>
var s = selects;
//获取对象
var p = document.getElementsByName(‘pro’)[0];//省
var c = document.getElementsByName(‘city’)[0];//市
var a = document.getElementsByName(‘area’)[0];//区
//绑定数据
s.bind(p,province);
s.bind(c,city);
s.bind(a,area);
//确定从属关系
s.parent(p,c);
s.parent(c,a);
//设置默认值
</script>
<?
}
else
{
?>
<script type=”text/javascript”>
var s = selects;
//获取对象
var p = document.getElementsByName(‘pro’)[0];//省
var c = document.getElementsByName(‘city’)[0];//市
var a = document.getElementsByName(‘area’)[0];//区
//绑定数据
s.bind(p,province);
s.bind(c,city);
s.bind(a,area);
//确定从属关系
s.parent(p,c);
s.parent(c,a);
//设置默认值
s.selected(p,{value:’<?=$ecmsfirstpost==1?”":htmlspecialchars(stripSlashes($r[pro]))?>’});
s.selected(c,{value:’<?=$ecmsfirstpost==1?”":htmlspecialchars(stripSlashes($r[city]))?>’});
s.selected(a,{text:’<?=$ecmsfirstpost==1?”":htmlspecialchars(stripSlashes($r[area]))?>’});
</script>
<?
}
?>
MySQL 默认使用的是MyISAM。但是,很多情况下这都是一个很糟糕的选择,除非你在创建一个非常简单抑或实验性的数据库。外键约束或者事务处理对于数据完整性 是非常重要的,但MyISAM都不支持这些。另外,当有一条记录在插入或者更新时,整个数据表都被锁定了,当使用量增加的时候这会产生非常差的运行效率。
结论很简单:使用InnoDB。
2、使用PHP的mysql函数
PHP自产生之日就提供了MySQL库函数(or near as makes no difference)。很多应用仍然在使用类似mysql_connect、mysql_query、mysql_fetch_assoc等的函数,尽管PHP手册上说:
$username = $_POST["name"]; $password = $_POST["password"]; $sql = “SELECT userid FROM usertable WHERE username=’$username’ AND password=’$password’;”; // run query…
只要在username字段输入“admin’;–”,这样就会被黑到,相应的SQL语句如下:
SELECT userid FROM usertable WHERE username=’admin’;
对于PHP开发人员来说,MySQL可能是使用最广泛的数据库系统,但并不是唯一的选择。PostgreSQL和Firebird是最强有力的竞争者:这个两者都是开源的,而且都没有被公司收购。微软提供了sql server Express,甲骨文提供了10g Express,这两者都是企业级数据库的免费版本。有时候,对于一个较小的web应用或者嵌入式应用,SQLite也不失为一个可行的替代方案。
PDO::CASE_LOWER: 强制列名是小写.
PDO::CASE_NATURAL: 列名按照原始的方式
PDO::CASE_UPPER: 强制列名为大写.
PDO::ATTR_ERRMODE: 错误提示.
PDO::ERRMODE_SILENT: 不显示错误信息,只显示错误码.
PDO::ERRMODE_WARNING: 显示警告错误.
PDO::ERRMODE_EXCEPTION: 抛出异常.
PDO::ATTR_ORACLE_NULLS (不仅仅是ORACLE有效,别的数据库也有效): )指定数据库返回的NULL值在php中对应的数值。
PDO::NULL_NATURAL: 不变.
PDO::NULL_EMPTY_STRING: Empty string is converted to NULL.
PDO::NULL_TO_STRING: NULL is converted to an empty string.
PDO::ATTR_STRINGIFY_FETCHES: Convert numeric values to strings when fetching. Requires bool.
PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).
PDO::ATTR_AUTOCOMMIT (available in OCI, Firebird and MySQL): Whether to autocommit every single statement.
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY (available in MySQL): Use buffered queries.
例子中的$rs->setFetchMode(PDO::FETCH_ASSOC);是PDOStatement::setFetchMode(),对返回类型的声明。
有如下:
PDO::FETCH_ASSOC — 关联数组形式
PDO::FETCH_NUM — 数字索引数组形式
PDO::FETCH_BOTH — 两者数组形式都有,这是缺省的
PDO::FETCH_OBJ — 按照对象的形式,类似于以前的 mysql_fetch_object()
更多返回类型声明(PDOStatement::方法名)看手册。
插入,更新,删除数据,
$db->exec(“Delete FROM `xxxx_menu` where mid=43″);
PDOStatement->bindColumn() — Bind a column to a PHP variable
PDOStatement->bindParam() — Binds a parameter to the specified variable name
PDOStatement->bindValue() — Binds a value to a parameter
PDOStatement->closeCursor() — Closes the cursor, enabling the statement to be executed again.
PDOStatement->columnCount() — Returns the number of columns in the result set
PDOStatement->errorCode() — Fetch the SQLSTATE associated with the last operation on the statement handle
PDOStatement->errorInfo() — Fetch extended error information associated with the last operation on the statement handle
PDOStatement->execute() — Executes a prepared statement
PDOStatement->fetch() — Fetches the next row from a result set
PDOStatement->fetchAll() — Returns an array containing all of the result set rows
PDOStatement->fetchColumn() — Returns a single column from the next row of a result set
PDOStatement->fetchObject() — Fetches the next row and returns it as an object.
PDOStatement->getAttribute() — Retrieve a statement attribute
PDOStatement->getColumnMeta() — Returns metadata for a column in a result set
PDOStatement->nextRowset() — Advances to the next rowset in a multi-rowset statement handle
PDOStatement->rowCount() — Returns the number of rows affected by the last SQL statement
PDOStatement->setAttribute() — Set a statement attribute
PDOStatement->setFetchMode() — Set the default fetch mode for this statement
PDOStatement->bindColumn() — Bind a column to a PHP variable
PDOStatement->bindParam() — Binds a parameter to the specified variable name
PDOStatement->bindValue() — Binds a value to a parameter
PDOStatement->closeCursor() — Closes the cursor, enabling the statement to be executed again.
PDOStatement->columnCount() — Returns the number of columns in the result set
PDOStatement->errorCode() — Fetch the SQLSTATE associated with the last operation on the statement handle
PDOStatement->errorInfo() — Fetch extended error information associated with the last operation on the statement handle
PDOStatement->execute() — Executes a prepared statement
PDOStatement->fetch() — Fetches the next row from a result set
PDOStatement->fetchAll() — Returns an array containing all of the result set rows
PDOStatement->fetchColumn() — Returns a single column from the next row of a result set
PDOStatement->fetchObject() — Fetches the next row and returns it as an object.
PDOStatement->getAttribute() — Retrieve a statement attribute
PDOStatement->getColumnMeta() — Returns metadata for a column in a result set
PDOStatement->nextRowset() — Advances to the next rowset in a multi-rowset statement handle
PDOStatement->rowCount() — Returns the number of rows affected by the last SQL statement
PDOStatement->setAttribute() — Set a statement attribute
PDOStatement->setFetchMode() — Set the default fetch mode for this statement