《PHP程序设计》上机期末考试试题
一、调试下列 各小题的输入结果(每题10分,共34分)
1、<php
    $a=1;
    function test(){
      echo $a;  }
    test();
>
调试答案:____________________________.
2、<php
    $b=201;
$c=40;
$a=$b>$c4:5;
echo $a;
>
调试答案:____________________________.
3、<php
    $arr=array(5=>1,12=>2);
    $arr[]=3;
    $arr["x"]=4;           
    print_r($arr); echo "<br>";
    unset($arr[5]);
    print_r($arr); echo <br>;
    unset($arr);
    print_r($arr);
  >
调试答案:____________________________.
4、<php
    $i=1;
    for(;;){
    if($i>10)  break;
  echo $i++." ";}
    echo "<br>";
>
调试答案:____________________________.
5、<php
    $m=2;$n=3;
  $table="<table border='1'>";
  for($i=1;$i<=$m;$i++){
      $table.="<tr>";
      for($j=1;$j<=$n;$j++){
      $table .= "<td>m:$i, n:$j</td>";
      }
    $table .= "</tr>";
  }
  $table .= "</table>";
  echo $table;
>
调试答案:____________________________.
6、<php
    $id=gzopen("","w");
    $id=gzopen("","r");
  echo gzgetc($id)."<br>";
    echo gzgets($id,4)."<br>";
  gzclose($id);$id=gzopen("","w"); 
    gzputs($id,"<html><title>this is a test.</title>\n");
  gzputs($id,"</html>the second line.\n");
    gzclose($id);$id=gzopen("","r");
  echo gzgetss($id,10)."<br>";
  gzpassthru($id); echo "<br>";
    gzclose($id);
>
调试答案:____________________________.
二、编程题及程序调试(每题20分 共计40分)
1、有一个数组$a=array(4,3,8,9,2),将其重新排序并输入结果,按从小到大的顺序排列。
1、<php
$a = array(4,3,8,9,2);
$j = 1; $temp = 0;
  while($j < count($a)) {
  for ($i=0; $i<count($a)-$j; $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);
//关闭数据库连接
php笔试题库mysqli_close($dbconn);
>
<php
$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]."<br>";
}
>
答案
一: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 test.the second line.
三、
1、<php
$a = array(4,3,8,9,2);
$j = 1; $temp = 0;
  while($j < count($a)) {
  for ($i=0; $i<count($a)-$j; $i++) {
    if($a[$i] > $a[$i+1]) {
    $temp = $a[$i];
    $a[$i] = $a[$i+1];
    $a[$i+1] = $temp;
    }
  }
  $j++;
  }
print_r($a);
>
2、<php
//用户注册事务开始
$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);
>
<php
$conn = mysql_connect('localhost','root','123');
mysql_select_db('test');
$query = "SELECT * FROM user";
$result = mysql_query($query,$conn);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。