浏览 2188 次
锁定老帖子 主题:php基础
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-12-15
最后修改:2011-12-15
环境:appSer包(打包的php,mySQL,apache);地址www.AppServNetwork.com; 目录:在www目录下发现了index.php 这里应该就是根目录吧。 资料:打开网页 www.w3school.com.cn/php 进行参考; 工具:文本编辑器EdiPlus开始尝试; IE打开地址: localhost/index.php 以下是练习代码: 基础 <html> <head> <title>jack's hello world!</title> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> <?php //基础 function testBase(){ $name="jack"; $content="hello world!"; $num1="2"; $num2="3"; //循环 echo "循环while"."<br>"; $num="1"; while($num<=10){ echo "number++:".$num."<br>"; $num++; } echo "循环for<br>"; for($n=11;$n>=$num;$n--){ echo "number--:".$num."<br>"; $num--; if($num<=0){ break; } } //数组 $arr=array($name,$content); echo "arrayList>>".$arr[0]." say: ".$arr[1]."<br>"; $hashMap=array("name"=>$name,"content"=>$content); echo "hashMap>>".$hashMap['name']." say: ".$hashMap['content']."<br>"; $cllection=array( "list"=>$arr, "map"=>$hashMap ); echo "collection:".$cllection['map']['name']."<br>"; echo "collection2:".$cllection['list'][1]."<br>"; //等比较 if($num1!=$num2){ echo "等比较>>$num1 != $num2<<"."<br>"; } if($name!=$content){ echo "等比较>>$name != $content<<"."<br>"; } //运算及简单str函数 echo "运算>>$num1 * $num2<<".($num1 * $num2)."<br>"; echo $content."函数>>strLen= ". strLen($content)."<br>"; echo $content."函数>>strPos(hello world!,!)= ". strPos($content,"!")."<br>"; } ?> <?php //表单处理 function testForm(){ //method="get" 明显的好处是可以加标签收藏网页(虽然没做过这个功能) echo "name:".$_REQUEST["name"]."<br>"; echo "content:".$_REQUEST["content"]; echo '<form name="form1" action="jack.php" method="post">'; echo 'name<input name="name" />'; echo 'content<input name="content" />'; echo '<button type="submit" name="sub" >提交</button>'; echo '</form>'; } ?> <?php //日期 function testDate(){ echo date("Y-m-d")."<br>"; //mktime() 函数可为指定的日期返回 Unix 时间戳。 mktime(hour,minute,second,month,day,year); $tomorrow=mktime(0,0,0,date("m"),date("d")+1,date("Y")); echo "tomorrow is ".date("Y-m-d",$tomorrow); } ?> <?php //引用 function testInclude(){ require("sayHi.php");//推荐,如果出现错误则不执行下列代码 //include("sayHi.php"); echo "echo!"; sayHi("jack"); } ?> <?php //文件上传 function testFile(){ echo '<form action="upload_file.php" method="post" enctype="multipart/form-data"><?php //enctype="multipart/form-data" 二进制数据提交 ?> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>'; } ?> <?php //会话session function testSession(){ //session_start();//启动 $_SESSION['name']="jack"; echo $_SESSION['name']; unset($_SESSION['name']);//注销指定session //session_destroy();//注销所有session echo $_SESSION['name']; } ?> <?php //错误处理 die("错误!"); echo "ss"; //错误处理机制 //... ?> <?php //异常 function checkNum($number) { if($number>1) { throw new Exception("Value must be 1 or below"); } return true; } //在 "try" 代码块中触发异常 try { checkNum(2); //If the exception is thrown, this text will not be shown echo 'If you see this, the number is 1 or below'; } //捕获异常 catch(Exception $e) { echo 'Message: ' .$e->getMessage(); } ?> <?php //Filter ??? ?> </body> </html> 文件上传类 <?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["file"]["tmp_name"]."<br>"; //把文件复制到upload/文件夹下 if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } } ?> 操作数据库 <?php //创建连接 function getConn(){ $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } echo $con; } //mysql_close($con);//关闭连接 ?> <?php //创建数据库,表,关联,及测试数据 function createDB(){ $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } //my_db 创建数据库 if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } //创建表 mysql_select_db("my_db", $con);//在创建表之前,必须首先选择数据库。通过 mysql_select_db() 函数选取数据库 $sql = "CREATE TABLE Persons ( FirstName varchar(15), LastName varchar(15), Age int )"; mysql_query($sql,$con); //建立关联 $sql = "CREATE TABLE Persons ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(15), LastName varchar(15), Age int )"; mysql_query($sql,$con); //插入值 mysql_select_db("my_db", $con); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')"); //关闭连接 mysql_close($con); } ?> <?php //插入值 function insert($firstname,$lastname,$age){ $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con);//选定数据库 $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$firstname','$lastname','$age')"; if (!mysql_query($sql,$con)) //执行SQL { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con); } ?> <?php //查询 function select(){ $con = mysql_connect("localhost","root","root"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); while($row = mysql_fetch_array($result)) { echo "|".$row['FirstName'] . "|" . $row['LastName']."|".$row['Age']."|"; echo "<br />"; } mysql_close($con); } ?> <?php //表单处理 function testForm(){ //method="get" 明显的好处是可以加标签收藏网页(虽然没做过这个功能) if($_REQUEST["firstname"]!=""){ insert($_POST[firstname],$_POST[lastname],$_POST[age]); } echo '<form name="form1" action="phpMySQL.php" method="post">'; echo 'firstname<input name="firstname" />'; echo 'lastname<input name="lastname" />'; echo 'age<input name="age" />'; echo '<button type="submit" name="sub" >提交</button>'; echo '</form>'; } ?> <html> <body> <?php testForm(); ?> <?php select();//怎么实现点击查询事件 ?> </body> </html> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |