懒人要有懒人的方式,下面就是在项目开发中常用的操作,你只需要复制粘贴即可噢。
1、数据库连接(connect database)
//根据实际的服务器参数来填写,一般的localhost是不需要改变的,
$conn=mysql_connect('localhost', 'demo', 'demo') or die("Can't connect to mysql host");
//选择要使用的数据库mysql_select_db('ipodmp') or die("Can't connect to DB");
?>
注:一般情况我们都是吧这段代码写入到conn.php文件内,然后在每一页内引用之,用法为在每一页开头加入:<?php include_once "condb.php";?>
2、检查数据表是否存在(check table exist)
function table_exists ($table, $db) {
$tables = mysql_list_tables ($db);
while (list ($temp) = mysql_fetch_array ($tables)) {
if ($temp == $table) { return TRUE; }
}
return FALSE;
}
如下调用
if (table_exists(test_table, my_database)) {
echo “Yes the table is there.”;
}
3、纪录读取(read records)
$query = “SELECT * FROM class order by ID desc”;
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row[‘ID’];
echo $row[‘Name’];
}
?>
4、纪录更新(updata records)
$ID = $_POST[‘IDs’];
$Content = $_REQUEST[‘content1’];
$query = “UPDATE config SET Content = ‘$Content’ WHERE ID =$ID”;
$result = mysql_query($query) or die(mysql_error());
?>
5、记录删除(delet records)
$ID = $_REQUEST[‘IDs’];
$query = “DELETE FROM class WHERE ID = $ID”;
$result = mysql_query($query) or die(mysql_error());
?>
6、记录插入(insert records)
$Name = $_REQUEST[‘names’];
$Model = $_REQUEST[‘Model’];
$Class = $_REQUEST[‘Class’];
$query=”INSERT INTO products(Name,Model,Class) VALUES (‘$Name’,’$Model’,’$Class’)”;
$result = mysql_query($query) or die(mysql_error());
?>
备注:
I.4、5 、6记录的读取,更新,删除,插入操作整体上比ASP操作来的简洁,只需要这样一种语法,一种方式,而ASP却有好几种语法,好几种方式,参数还一堆。
II.个人觉得PHP中的语法比较简洁,对于开发时能加速,如上面的变量替换吧,很简洁,ASP写出来的代码就多了。
7、SESSION的使用
PHP的SESSION使用特别麻烦,因为他有设置开关,要用时,需要打开,要不用不了,谁知道能写一次代码,就不用次次都写吗?
要使用SESSION的时候,需要使用 session_start(); 来启用,然后再使用变量,如下
if($_SESSION[‘Login’] != true){
header(“location:index.php”);
}