最近打算学习下PHP,总的来说,PHP相对来讲还是简单易学的,最近多少学习了一些点,可以做一个总结,以后有个参考
首先,作为快速开发的解释语言,PHP在WEB开发方面 有着强有力的竞争力,一般作为企业门户,BBS,CMS,电子商务类的用途广泛
特别的是CGI出现以后,PHP的效率有了大量的提升,这样也加速了它的普及使用率。
其实学习语言,一般开始就是Hello Word,但是对于工作多年的人来说,这个太小儿科了。。。。
PHP是跨平台,弱类型的语言,这点类似JS,夸平台:主要是在主流的OS上,都有PHP的解释器PHP SDK ,当然跨平台一般都是这个套路。PHP可以与HTML集成,嵌入到页面中,这点类似于JSP页面写JAVA代码,既然能和HTML集成,那么自然也支持JS。。看下面的示例代码:
<script lang="javascript"> function checkUserAndPwd() { var uname = loginForm.username.value; if (uname == "") { alert("username not allow null!"); document.getElementById("username").focus(); return false; } var password = loginForm.password.value; if (password == "") { alert("password not allow null!"); document.getElementById("password").focus(); return false; } return true; } </script> <html> <head> <meta charset="UTF-8"> <title>个人日常积分系统</title> </head> <body onload="javascript:document.getElementById('password').value='';"> <?php echo "打算开发一个积分系统来练练手...这是首页"; ?> <div> <form id="loginForm" name="loginForm" method="post" action="jifen.php" > <input autocomplete="off" type="input" name="username" id="username" value=""/> <br> <input autocomplete="off" type="password" name="password" id="password" value=""/> <br> <input type="submit" name="submit" value="现在登录" onclick="return checkUserAndPwd();"/> <a id="regist" href="regist.php" id="regist">还没帐号?立即注册</a> </form> </div> </body> </html>
HTTP提交有post,get,等多种方式,但是常用的就是这两种,通过Form表单的method="post" 这个参数来决定,对于Post,后台获取参数的值的办法如下:
<?php $uname = $_POST["username"]; $pawd = $_POST["password"]; ?
而对于采用Get这种类型比如URL重写这种,后台使用$_GET[“name"]来获取
<?php $uname = $_GET["username"]; $pawd = $_GET["password"]; ?
这个是获取参数的例子,那么如果我要实现一个注册用户的功能改怎么样呢?其实就是要连接数据库了,如果是用Mysql,那么新手的例子如下:
regist.php
<!DOCTYPE html> <script lang="javascript"> function checkUserAndPwd(){ var uname=registForm.username.value; if(uname ==""){ alert("username not allow null!"); document.getElementById("username").focus(); return false; } var password=registForm.password.value; var password2=registForm.password2.value; if(password =="" ||password2 ==""){ alert("password not allow null!"); document.getElementById("password").focus(); return false; } var phone=registForm.phone.value; if(phone ==""){ alert("phone not allow null!"); document.getElementById("phone").focus(); return false; } return true; } </script> <html> <head> <meta charset="UTF-8"> <title>个人日常积分系统</title> </head> <body> <?php echo "打算开发一个积分系统来练练手...这是注册页面"; ?> <div> <form id="registForm" name="registForm" method="post" action="registprocess.php" > 用户名: <input type="input" name="username" id="username" value=""/> <br> 密码: <input type="password" name="password" id="password" value=""/> <br> 确认密码:<input type="password" name="password2" id="password2" value=""/> <br> 手机号码: <input type="input" name="phone" id="phone" value=""/> <br> <input type="submit" name="submit" onclick="return checkUserAndPwd();" value="现在注册"/> <a id="regist" href="index.php" id="regist">已有帐号?立即登录</a> </form> </div> </body> </html>
提交到处理页面
<html> <head> <meta charset="UTF-8"> </head> <body> <?php echo "打算开发一个积分系统来练练手...这是注册处理页面"; echo "<br>"; $username = $_POST["username"]; echo "你输入的用户名是:" . $_POST["username"]; echo "<br>"; $password = $_POST["password"]; echo "你输入的密码是:" . $_POST["password"]; echo "<br>"; $phone = $_POST["phone"]; echo "你输入的手机号码是:" . $_POST["phone"]; ?> <?php $dbname = "mysql"; echo "<br>"; $link = @mysql_connect("localhost", "root", "root123") or die("不能连接数据库" . mysql_error()); echo "<br>"; echo "$link"; echo "<br>"; if ($link) { // echo "数据库连接成功"; // echo "<br>"; } $db_selected = mysql_select_db($dbname, $link); // $db_selected= mysql_query($dbname,$link); //等价上面 if ($db_selected) { echo "<br>"; echo "数据库连接成功"; echo "<br>"; echo "你输入的用户名是:" . $username; echo "<br>"; } $sql = "insert into jf_user(uname,pwd,phone,bumen,isadmin) values ('$username','$password','$phone','研发中心','N')"; $jifen = "insert into jf_jifen (uname,fenshu,yuanying) values ('$username',100,'initOk')"; echo $sql; mysql_query("set names utf8"); $insertResult = mysql_query($sql, $link); echo "<br>"; echo $insertResult; if($insertResult){ $init = mysql_query($jifen, $link); } echo "<script>alert('regist ok ');window.location.href='index.php';</script>"; // mysql_free_result($insertResult); mysql_close($link); ?> </body> </html>
这里是连接数据库的代码,其中主要的:
连接Mysql数据库,不能连接则提示错误
// $db_selected= mysql_query($dbname,$link); //等价上面
if ($db_selected) {
为True,表示连接成功
$insertResult = mysql_query($sql, $link);
if($insertResult){
$init = mysql_query($jifen, $link);
}
echo "<script>alert('regist ok ');window.location.href='index.php';</script>";
执行SQl,判断是否成功,然后提示用户并返回,
最后,关闭连接:
// mysql_free_result($insertResult);
mysql_close($link);
这注册好了,我要登陆,这个时候首先要验证用户和密码:当然到数据库里取出来和用户的输入做比较
<php? $uname = $_POST["username"]; $pawd = $_POST["password"]; ?>
连接数据库,查询用户是否存在:
<?php $dbname = "mysql"; $link = @mysql_connect("localhost", "root", "root123") or die("不能连接数据库" . mysql_error()); $db_selected = mysql_select_db($dbname, $link); $sql = "select * from jf_user where uname = '$uname'"; $result = mysql_query($sql); $list = mysql_fetch_array($result); if (strcmp($pawd, $pwd) != 0) { echo "<script> alert('用户名或者密码错误!');history.back();</script>"; } else { $_SESSION["user"] = $uname; } ?>
关于便利查询结果:这里简单介绍两个:mysql_fetch_array mysql_fetch_object
mysql_fetch_object如何取值:
do { echo "<br>"; echo "数据库取出来的用户是 :" . $list2->uname; echo "<br>"; echo "数据库取出来的分数是 :" . $list2->fenshu; echo "<br>"; echo "得分原因:" . $list2->yuanying; } while ($list2 = mysql_fetch_object($jifenresult));
mysql_fetch_array 如何取值:
do { echo "<br>"; echo "数据库取出来的用户是 :" . $listAll[uname] . " " . "<a href='edit_jifen.php?uname=$listAll[uname]&fenshu=$listAll[fenshu]&yy=$listAll[yuanying]' > 编辑</a>"; echo "<br>"; echo "数据库取出来的分数是 :" . $listAll[fenshu]; echo "<br>"; echo "得分原因 :" . $listAll[yuanying]; echo "<br>"; } while ($listAll = mysql_fetch_array($allList));
来个综合的:
if (strcmp($uname, 'admin') != 0) { do { echo "<br>"; echo "数据库取出来的用户是 :" . $list2->uname; echo "<br>"; echo "数据库取出来的分数是 :" . $list2->fenshu; echo "<br>"; echo "得分原因:" . $list2->yuanying; } while ($list2 = mysql_fetch_object($jifenresult)); } else { do { echo "<br>"; echo "数据库取出来的用户是 :" . $listAll[uname] . " " . "<a href='edit_jifen.php?uname=$listAll[uname]&fenshu=$listAll[fenshu]&yy=$listAll[yuanying]' > 编辑</a>"; echo "<br>"; echo "数据库取出来的分数是 :" . $listAll[fenshu]; echo "<br>"; echo "得分原因 :" . $listAll[yuanying]; echo "<br>"; } while ($listAll = mysql_fetch_array($allList)); echo "<br>"; } mysql_close($link);