2024年2月3日发(作者:)
《PHP程序设计》上机期末考试试题
一、 调试下列 各小题的输入结果(每题10分,共34分)
1、 $a=1; function test(){ echo $a; } test(); > 调试答案:____________________________. 2、 $b=201; $c=40; $a=$b>$c4:5; echo $a; > 调试答案:____________________________. 3、 $arr=array(5=>1,12=>2); $arr[]=3; $arr["x"]=4; print_r($arr); echo " "; unset($arr[5]); print_r($arr); echo “ ”; unset($arr); print_r($arr); > 调试答案:____________________________. 4、 $i=1; for(;;){ if($i>10) break; echo $i++." ";} echo " "; > 调试答案:____________________________. 5、 $m=2;$n=3; $table=" for($i=1;$i<=$m;$i++){ $table.=" for($j=1;$j<=$n;$j++){ $table .= " } $table .= " } $table .= " echo $table; > 调试答案:____________________________. 6、 $id=gzopen("","w"); $id=gzopen("","r"); echo gzgetc($id)." "; echo gzgets($id,4)." "; gzclose($id);$id=gzopen("","w"); gzputs($id," gzputs($id,"the second line.n"); gzclose($id);$id=gzopen("","r"); echo gzgetss($id,10)." "; gzpassthru($id); echo " "; gzclose($id); > 调试答案:____________________________. 二、 编程题及程序调试(每题20分 共计40分) 1、有一个数组$a=array(4,3,8,9,2),将其重新排序并输入结果,按从小到大的顺序排列。 1、 $a = array(4,3,8,9,2); $j = 1; $temp = 0; while($j < count($a)) { for ($i=0; $i if($a[$i] > $a[$i+1]) { $temp = $a[$i]; $a[$i] = $a[$i+1]; $a[$i+1] = $temp; } } $j++; } print_r($a); > 2、数据库qzh中有一个表user,其结构为(name,tel,content,date),已有如下三条记录 2006-10-11 2006-10-15 2006-10-15 请使用php编写程序实现: (1)查询所有姓名为“张三”的记录,并使用mysql_fetch_array函数输出查询结果; (2)添加2007-05-06)至表中; (3)张三的时间更新成为当前系统时间。 注意:请将以上三个操作作为一个事务来处理,即以上三个操作全部成功时,才提交事务,只要有一个操作失败,其余两个操作不能提交。 $dbconn = mysqli_connect("localhost", "root", "123", "test") or die("不能连接到数据库"); mysqli_autocommit($dbconn, false); //查询user表中所有姓名为“张三”的记录 $sql1 = "select * from user where name='张三'"; $result = mysqli_query($dbconn, $sql1); if($result != true) mysqli_rollback($dbconn); //如果出错,则回滚到开始状态 2007-05-06)至表中 $result = mysql_query($dbconn,$sql2); if($result != true) mysqli_rollback($dbconn); //如果出错,则回滚到开始状态 //更改张三的毕业时间为当前系统时间 $sql3 = "update user set nf=now() where name='张三'"; $result = mysqli_query($dbconn, $sql3); if($result != true) mysqli_rollback($dbconn); //如果出错,则回滚到开始状态 //没有任何错误,则提交,完成一次事务操作 mysqli_commit($dbconn); //关闭数据库连接 mysqli_close($dbconn); > $conn = mysql_connect('localhost','root','123'); mysql_select_db('test'); $query = "SELECT * FROM user"; $result = mysql_query($query,$conn); //使用mysql_fetch_array函数输出查询结果 while($row = mysql_fetch_array($result)){ echo $row[0]." "; echo $row[1]." "; echo $row[2]." "; echo $row[3]." "; } > 答案 一:D B B D C C B D B D 二:1、无输出 2、4 3、Array ( [5] => 1 [12] => 2 [13] => 3 [x] => 4 ) Array ( [12] => 2 [13] => 3 [x] => 4 ) 4、1 2 3 4 5 6 7 8 9 10 5、 m:1, n:1 m:1, n:2 m:1, n:3 m:2, n:1 m:2, n:2 m:2, n:3 6、1 234 tle>this is a second line. 三、 1、 $a = array(4,3,8,9,2); $j = 1; $temp = 0; while($j < count($a)) { for ($i=0; $i if($a[$i] > $a[$i+1]) { $temp = $a[$i]; $a[$i] = $a[$i+1]; $a[$i+1] = $temp; } } $j++; } print_r($a); > 2、 //用户注册事务开始 $dbconn = mysqli_connect("localhost", "root", "123", "test") or die("不能连接到数据库"); mysqli_autocommit($dbconn, false); //查询user表中所有姓名为“张三”的记录 $sql1 = "select * from user where name='张三'"; $result = mysqli_query($dbconn, $sql1); if($result != true) mysqli_rollback($dbconn); //如果出错,则回滚到开始状态 2007-05-06)至表中 $result = mysqli_query($dbconn,$sql2); if($result != true) mysqli_rollback($dbconn); //如果出错,则回滚到开始状态 //更改张三的毕业时间为当前系统时间 $sql3 = "update user set nf=now() where name='张三'"; $result = mysqli_query($dbconn, $sql3); if($result != true) mysqli_rollback($dbconn); //如果出错,则回滚到开始状态 //没有任何错误,则提交,完成一次事务操作 mysqli_commit($dbconn); //关闭数据库连接 mysqli_close($dbconn); > $conn = mysql_connect('localhost','root','123'); mysql_select_db('test'); $query = "SELECT * FROM user"; $result = mysql_query($query,$conn); //使用mysql_fetch_array函数输出查询结果 while($row = mysql_fetch_array($result)){ echo $row[0]." "; echo $row[1]." "; echo $row[2]." "; echo $row[3]." "; } > ";
"; "; "; m:$i, n:$j ";
本文发布于:2024-02-03 22:05:51,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170696915151433.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |