PHP and MySQL Web Development习题作业集

本文档记录了从PHP和MySQL的安装配置到数据库操作的学习过程,包括Ubuntu上安装MySQL,PHP及PHP-FPM的配置,以及PHP访问MySQL数据库的基础知识。详细介绍了数组、字符串处理、文件操作、面向对象编程、错误和异常处理、数据库设计和操作等核心概念。通过实例展示了如何创建和管理数据库,以及使用PHP进行数据库查询和操作。
摘要由CSDN通过智能技术生成

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
The definitive guide to building database-driven Web applications with PHP and MySQL PHP and MySQL are popular open-source technologies that are ideal for quickly developing database-driven Web applications. PHP is a powerful scripting language designed to enable developers to create highly featured Web applications quickly, and MySQL is a fast, reliable database that integrates well with PHP and is suited for dynamic Internet-based applications. PHP and MySQL Web Development shows how to use these tools together to produce effective, interactive Web applications. It clearly describes the basics of the PHP language, explains how to set up and work with a MySQL database, and then shows how to use PHP to interact with the database and the server. This practical, hands-on book consistently focuses on real-world applications, even in the introductory chapters. The authors cover important aspects of security and authentication as they relate to building a real-world website and show you how to implement these aspects in PHP and MySQL. They also introduce you to the integration of front-end and back-end technologies by using JavaScript in your application development. The final part of this book describes how to approach real-world projects and takes the reader through the design, planning, and building of several projects, including: User authentication and personalization Web-based email Social media integration The fifth edition of PHP and MySQL Web Development has been thoroughly updated, revised, and expanded to cover developments in PHP through versions 5.6 and 7, as well as features introduced in recent stable releases of MySQL. Table of Contents Part I: Using PHP Chapter 1 PHP Crash Course Chapter 2 Storing and Retrieving Data Chapter 3 Using Arrays Chapter 4 String Manipulation and Regular Expressions Chapter 5 Reusing Code and Writing Functions Chapter 6 Object-Oriented PHP Chapter 7 Error and Exception Handling Part II: Using MySQL Chapter 8 Designing Yo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值