php和数据库的学习,来自于书本PHP and MySQL web Development.每章都是重点介绍还有些代码。
ubuntu安转mysql
Ubuntu上安装MySQL非常简单只需要几条命令就可以完成。
1. sudo apt-get install mysql-server
2. apt-get isntall mysql-client
3. sudo apt-get install libmysqlclient-dev
安装过程中会提示设置密码什么的,注意设置了不要忘了,安装完成之后可以使用如下命令来检查是否安装成功:
sudo netstat -tap | grep mysql
通过上述命令检查之后,如果看到有mysql 的socket处于 listen 状态则表示安装成功。
登陆mysql数据库可以通过如下命令:
mysql -u root -p
-u 表示选择登陆的用户名, -p 表示登陆的用户密码,上面命令输入之后会提示输入密码,此时输入密码就可以登录到mysql。
安装php
apt-get install php5
安装php-fpm
apt-get install php5-fpm
修改nginx配置文件,碰到php的传递到php处理
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/php/$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
}
记得 nginx -s reload 重启生效
修改php-fpm配置文件
vim /etc/php5/fpm/php-fpm.conf
增加端口处理,
listen = 127.0.0.1:9000
记得重启生效
/etc/init.d/php5-fpm restart
验证
新建/data/php/ 目录下 index.php文件
<?php
echo "php"
?>
输入网址
http://47.93.38.195/index.php
回复php表示成功
静态的网址成功,接下来需要动态的数据,实现数据的上传和返回。
首先还是需要配置nginx.conf文件
location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/php/$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REDIRECT_STATUS 200;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
}
fastcgi_param REDIRECT_STATUS 200;
参数不知道什么意思,因为不知道,因为这个参数获取的post值为空,网上到处查资料也没有教给我,还是要多看nginx配置手册才是,网上讲得乱七八糟的。
编写orderform.php文件
<form action="processorder.php" method="post">
<table style="border: 0px;">
<tr style="background: #cccccc;">
<td style="width: 15px; text-align: center;">Item</td>
<td style="width: 15px; text-align: center;">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td><input type="text" name="tireqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td>Oil</td>
<td><input type="text" name="oilqty" size="3" maxlength="3" /></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td><input type="text" name="sparkqty" size="3" maxlength="3"/></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<input type="submit" value="Submit Order" /></td>
</tr>
</table>
</form>
编写它的处理函数processorder.php
<!DOCTYPE html>
<html>
<head>
<title>
Bob's Auto Parts - Order Results
</title>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo '<p>Order processed.</p?>';
?>
<?php
echo "<p>Order processed at ".date('H:i, jS F Y')."</p>";
?>
<?php
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
?>
<?php
echo '<p>Your order is as followes:</p>';
echo htmlspecialchars($tireqty).' tires<br/>';
echo htmlspecialchars($oilqty).' bottles of oil<br/>';
echo htmlspecialchars($sparkqty).' spark plugs<br />';
?>
</head>
</html>
输入http://47.93.38.195/orderform.php
输入数据
CTYPE html>
Bob's Auto Parts
Order Results
Order processed.
Order processed at 20:55, 20th August 2017
Your order is as followes:
5 tires
48 bottles of oil
20 spark plugs
至此,动态网站已经建立。
Introduction
Reading this book will enable you to build real-world, dynamic web application. If you've built websites using plain HTML, you realize the limitations of this approach.
Static content from a pure HTML. website is just that -- static. It stays the same unless you physically update it.
Chapter 1 PHP Crash Course
1. This chapter begins with the example of an online product order form to show how variables, operators, and expressions are used in PHP. It also covers variables types and operator precedence. You will learn how to access form variables and manipulate them by working out the total and tax on a customer order.
2. The PHP has been interpreted and executed on the web server, as distinct from JavaScript and other client-side technologies interpreted and executed within a web browser on a user's machine.
3. The PHP code in the preceding example began with <?php and ended with ?>
Any text between the tags is interpreted as PHP.
Any text outside these tags is treated as normal HTML.
4. Semicolons separate statements in PHP much like periods separate sentences in English.
5. You can recognize variable names in PHP because they all start with a dollar sign($).
$_POST['tireqty]
$_POST is an array containing data submitted via a HTTP POST request - that is, the form method was set to POST.
There are three of these arrays that may contain form data: $_POST, $_GET, $_REQUEST.
5. The following statement creates a new variable name $tireqty and copies the contents of $_POST['tireqty] into new variable
$tireqty = $_POST['tireqty];
6. The period is the string concatenation operator, which adds strings (pieces of text) together.
echo htmlspecialchars($tireqty). ' tires<br/>';
7.或者
$tireqty = htmlspecialchars($tireqty);
echo "$tireqty tires<br/>";
8. PHP tries to evaluate strings in double quotation marks, resulting in the behavior shown earlier. Single-quoted strings are treated as true literals.
9. Data type
Integer Float String Boolean Array Object
NULL, resource, callable
Callables are essentially functions that are passed to other functions.
主要讲述基本的语法和前面学习过的语言类似,所以后面看代码再熟悉。
第二章 数据存储
这章主要讲述open write read close 与文件相关的函数,进行数据的存储。
在使用fopen在服务器创建文件的时候,首先会碰到权限不够的问题。
第一步就是手动在服务器上创建文件,文件的目录和文件名如果没有指定document_root的话,就用绝对路径,
然后还要修改文件的权限,一般默认文件的权限是644,所以这里需要修改成666
用以下命令
chmod 666 orders.txt
完整的文件读写的php示例
<?php
// create short variable names
$tireqty = (int) $_POST['tireqty'];
$oilqty = (int) $_POST['oilqty'];
$sparkqty = (int) $_POST['sparkqty'];
$address = preg_replace('/\t|\R/', ' ', $_POST['address']);
$document_root = $_SERVER['DOCUMENT_ROOT'];
$date = date('H:i, jS F Y');
?>
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1&g