关于PHP入门学习

由于项目到了收尾的阶段,时间上都充裕得多,上班的时候多半是 Fix Bugs 为主。闲的无聊时,突然兴致一起,到网上搜了一下 PHP 的资料,开始了两天的 PHP 的学习之旅。在这里要衷心的感谢给我提供帮助的从事 PHP 开发的曾经的室友 --- 东哥。

因为学习的时间比较短,谈不上对 PHP 有比较高的造诣。只是在这里谈谈一下自己的学习经验,并且做一些记录,留作自己以后参考。

 

学习过程:

1.          PHP 的基本语法。

2.          SMARTY 的使用。

3.          环境的搭建。

4.          第一个示例。

 

首先 SHOW 一下这两天的学习成果。

 

 

关于PHP入门学习_第1张图片

 

关于PHP入门学习_第2张图片

 

 

 

很简单,就是一个 MYSQL 的 CRUD 。通过 SMARTY 实现了形式与功能的分离,并且简单地实现了多语言。

 

好了,让我们正式开始了。主要是围绕最终的实例开展的。

 

(一)   环境的部署。

IDE : DREAMWEAVER 8, 但是 DW8 对程序的调试支持不够,建议下载一个 ECLIPSE 的 PHP 插件。

应用服务器: appserv-win32-2.5.9 。这个是集成的环境,内含 APACHE 、 MySql 等,可以自由选择需要的组件进行安装。

 

工程结构图:

 

 

 关于PHP入门学习_第3张图片

 

软件安装成功后需要启动应用服务器和数据库:

 

 

 关于PHP入门学习_第4张图片

 

(二)   基本的 PHP 语法

应该说,这类型的资料比较多,要说一本书才足够。建议新人自己下去查看一下 PHP 的学习手册,和快速入门等资料。下面给推荐一个网站:

http://www.w3school.com.cn/php/

主要学习变量,类,表单等内容。

 

(三)   数据库的链接

我自己写一个数据库的访问的类,可以实现基本的操作,只是针对我的实例:

       /** MySQL settings - You can get this info from your web host **/

       define('DB_NAME', 'phptest');

        

       /** MySQL database username */

       define('DB_USER', 'root');

        

       /** MySQL database password */

       define('DB_PASSWORD', '123456');

        

       /** MySQL hostname */

       define('DB_HOST', 'localhost');

      

       class DataBaseAccess

       {

              var $conn;

             

              function DataBaseAccess()

              {

                     $this->getConnection();

              }

             

              function getConnection()

              {

                     $this->conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

                    

                     if (!($this->conn))

                     {

                            die('Could not connect: ' . mysql_error());

                     }

                     else

                     {

                            // echo " 数据库连接成功 ...";

                     }

                    

                     mysql_select_db(DB_NAME, $this->conn);

                    

              }

             

              function closeConnection()

              {

                     mysql_close($this->conn);

              }

             

              /**

                * Search the database table by id.

                */

              function findById($table, $id)

              {

                     $sql="select * from ". $table ." where id=".$id;

                     //Execute query

                     $result = mysql_query($sql);

                     if(!$result)

                     {

                            echo " 失败! ";

                            mysql_close($con);

                     }

                    

                     $student = mysql_fetch_row($result);

                     return $student;

              }

             

              /**

                * Search all the records the database table.

                */

                function findAll($table)

                {

                     $sql="SELECT * FROM ". $table;

                     //Execute query

                     $result = mysql_query($sql);

                    

                     if(!$result)

                     {

                            echo " 查找失败! ";

                            mysql_close($con);

                     }

                     return $result;

                }

               

                /**

                * Create the record.

                * $array must define as array('name'=>'test', 'number'=>'1111', 'sex'=>'male')

                */

                function create($table, $array)

                {

                     $columns;// name,number,sex

                     $values; // test,1111,male

                     foreach($array as $key=>$value)

                     {    

                            if($columns)

                            {

                                   $columns = $columns.",".$key;

                                   $values = $values.",'".$value."'";

                            }

                            else

                            {

                                   $columns = $key;

                                   $values = "'".$value."'";

/* 这里可以针对类型属性,判断用‘’还是不用等 */

                            }

 

                     }

                    

                     $sql="INSERT INTO ".$table." (".$columns.") values (".$values.")";

                     // echo $sql;

                     //Execute query

                     $result = mysql_query($sql);

                     if(!$result)

                     {

                            echo " 添加失败! ";

                            mysql_close($con);

                     }

                    

                     $newID = mysql_insert_id(); // 获取刚刚插入记录的 ID

 

                     return $newID;

 

                }

               

                /**

                * Update the record.

                * $array must define as array('name'=>'test', 'number'=>'1111', 'sex'=>'male')

                */

                function update($table, $array, $id)

                {

                     $column2value;

                     foreach($array as $key=>$value)

                     {

                            if($column2value)

                            {

                                   $column2value = $column2value. ',' .$key. "='" .$value. "'";

                            }

                            else

                            {

                                   $column2value = $key. "='" .$value. "'";

                            }

                           

 

                     }

                     $sql="UPDATE ".$table." SET ".$column2value ." WHERE id=". $id;

                     //Execute query

                     $result = mysql_query($sql);

                     if(!$result)

                     {

                            echo " 添加失败! ";

                            mysql_close($con);

                     }

 

                }

               

                /**

                * Search all the records the database table.

                */

                function delete($table, $id)

                {

                     $sql="delete from students where id=".$id;

                     //Execute query

                     mysql_query($sql);

                }

       }

 

?>

 

用法:(插入一条数据时)

       require_once('inc/conn.php');

       require_once('inc/common.php');

      

       $name = $_POST['name'];

       $number = $_POST['number'];

       $sex = $_POST['sex'];

      

 

       // Initial the connection of the database.

       $dba = new DataBaseAccess();

      

       $array = array(

              "name"=>$name,

              "number"=>$number,

              "sex"=>$sex

       );

      

       $newId = $dba->create('students', $array);

      

       // Close the connnection, release the resources.

       $dba->closeConnection();

      

      

       CommonUtil::trace("show.php?id=".$newId);

?>

 

你可能感兴趣的:(关于PHP入门学习)