php 命令行模式 参考 http://www.yesky.com/imagesnew/software/php/zh/features.commandline.html
使用php cli 命令行查看php 安装模块
php -m
# apt-get install php5-gd
# apt-get install php5-mysql
# /etc/init.d/apache2 restart
测试
1.连接与断开服务器
<?php
$db_host = "localhost"; //连接服务器地址 $db_user = "root"; //连接数据库的用户名 $db_psw = "root"; //连接数据库的密码 $connection = mysql_connect($db_host,$db_user,$db_psw); if(!$connection){ die('链接MySQL服务器失败!'); } echo '连接MySQL服务器成功!'; mysql_close($connection); ?><!-- 在默认请款下,脚本执行完毕会自动断开与服务器的连接,但是使用mysql_close()函数则可以在指定的位置来关闭连接释放内存。 -->2.选择数据库
<?php
$db_host = "localhost"; $db_user = "root"; $db_psw = "root"; $db_name = "sunyang"; $connection = mysql_connect($db_host,$db_user,$db_psw) or die("连接服务器失败"); mysql_select_db($db_name,$connection) or die("选择数据库失败"); mysql_close($connection); ?>3.执行查询
mysql_query()函数仅对SELECT、SHOW、EXPLAIN和DESCRIBE语句返回一个资源标识符,如果查询执行错误则返回FALSE。对于其他类型的SQL语句,mysql_query()在执行成功时返回TRUE,错误时返回FALSE。
4.获取查询结果<?php
$connection = mysql_connect("localhost","root","root"); mysql_select_db("sunyang",$connection) or die("选择数据库失败"); $query = "SELECT * FROM employee"; $result = mysql_query($query) or die("查询失败"); //执行查询 if(mysql_num_rows($result)>0){ //判断结果集中行的数目是否大于零 while(($row = mysql_fetch_array($result)) !=false){//通过while循环将数组中的值输出 echo $row['emp_id']; echo $row['emp_number']; echo $row['emp_name']; echo $row['emp_age']; } }else{ echo "未查询到任何记录"; } mysql_free_result($result); //释放结果集内存 mysql_close($connection); ?>5.分页处理
<?php
$connection = mysql_connect("localhost","root","root") or die("连接服务器失败"); mysql_select_db("sunyang",$connection) or die("选择数据库失败"); $query1 = "SELECT * FROM employee"; $result = mysql_query($query1) or die("查询数据失败");//执行查询 $nowsPerPage = 2; //每一页显示的行数 $maxRowCount = mysql_num_rows($result); //总行数 if($maxRowCount%$nowsPerPage == 0){ $maxPage = (int)($maxRowCount/$nowsPerPage); //计算总页数 }else{ $maxPage = (int)($maxRowCount/$nowsPerPage) + 1; } if(isset($_GET['curPage'])){ $page = $_GET['curPage']; //获取当前页数 }else{ $page = 1; } $start = $nowsPerPage*($page-1); //起始记录数 $query2 = "SELECT * FROM employee ORDER BY emp_id LIMIT $start,$nowsPerPage"; $result = mysql_query($query2) or die("查询数据失败");//执行查询 while(($row = mysql_fetch_array($result)) != false){ //通过while循环将数组中的值输出 echo $row['emp_id']; echo $row['emp_number']; echo $row['emp_name']; echo $row['emp_age']; } if($page >1){ //当前页不是第一页 $prevPage = $page - 1; //上一页 echo "<a href='?curPage=$prevPage'>上一页</a>"; } if($page < $maxPage){ $nextPage = $page + 1; //下一页 echo "<a href='?curPage=$nextPage'>下一页</a>"; } mysql_free_result($result); mysql_close($connection); ?>6.保存数据
<?php
$connection = mysql_connect("localhost","root","root"); mysql_select_db("sunyang",$connection); $query = "INSERT INTO employee(emp_number,emp_name,emp_age) VALUES('sy0804','employee4',27)"; if(mysql_query($query)){ echo "保存数据成功"; } else { echo "保存数据失败"; } mysql_close($connection); ?>7.修改数据
<?php
$connection = mysql_connect("localhost","root","root") or die("连接服务器失败"); mysql_select_db("sunyang",$connection) or die("选择数据库失败"); $query = "UPDATE employee SET emp_age=28 WHERE emp_id=1"; if(mysql_query($query)){ echo "修改数据成功,受影响的行数:".mysql_affected_rows(); }else{ echo "修改数据失败"; } mysql_close($connection); ?>8.删除数据
<?php
$connection = mysql_connect("localhost","root","root") or die("连接服务器失败"); mysql_select_db("sunyang",$connection); $query = "DELETE FROM employee WHERE emp_id=2"; if(mysql_query($query)){ echo "删除数据成功,受影响的行数:".mysql_affected_rows(); }else{ echo "删除数据失败"; } mysql_close($connection); ?>