假期从头过一遍php,每天记录学习笔记,希望能坚持下来!
一. 为静态页面赋予生命
1. 即使一个.php文件包含PHP代码,浏览器也绝对不会看到,它只会看到服务器上运行PHP代码所得到的HTML代码
2. PHP脚本必须在一个Web服务器上运行,否则将无法工作
3. FTP(File Transfer Protocol):文件传输协议
4. 命名规则(约定俗成):变量名都使用小写;用下划线分隔多词变量名中的各个词
5. $_POST是PHP内置的超级全局变量,在整个脚本中都可用。脚本一旦运行,$_POST就已经存在了。
6. 转义字符中,单引号只支持 \' 和 \\
7. PHP mail函数:mail($to, $subject, $msg, 'From:' . $email);
二. 连接MySQL
1. SQL(Structured Query Language):结构化查询语言
2. 用PHP脚本向MySQL中插入数据实例
$first_name = $_POST['firstname'];
$last_name = $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did = $_POST['whattheydid'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$other = $_POST['other'];
// mysqli_connect()
$dbc= mysqli_connect('localhost','root','','aliendatabase')
or die('Error connecting to MySQL server.');
$query = "INSERT INTO aliens_abduction (first_name, last_name, when_it_happened, how_long, " .
"how_many, alien_description, what_they_did, fang_spotted, other, email) " .
"VALUES ('$first_name', '$last_name', '$when_it_happened', '$how_long', '$how_many', " .
"'$alien_description', '$what_they_did', '$fang_spotted', '$other', '$email')";
// mysqli_query()
$result= mysqli_query($dbc,$query) or die('Error querying database.');
// mysqli_close()
mysqli_close($dbc);
?>
3. mysqli中的“i”代表改进(improved),现在更倾向于使用mysqli_系列中的函数
4. PHP die()函数会终止一个PHP脚本,并提供失败代码的反馈
5. mysqli_connect('数据库位置','用户名','密码','数据库名');
6. mysqli_query(database_connection,query); // qurey是一个查询串
7. mysqli_close(database_connection); // 用完数据库就将其关闭是一个好习惯
8. 数据库服务器同时只允许一定数目的可用连接,所以要尽可能地节省
9. 在处理表单数据之前先将其隔离是一个很好的编程习惯