OOP-c1:

<html>

<title></title>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<style>

table

{

border-collapse:collapse;

margin-left:50%;

margin-top:30%;

}

table td

{

border:1px #000 solid;

}

</style>

</head>

<body>

<table border='1'>

<tr>

<th>Id</th>

<th>Name</th>

<th>Age</th>

</tr>

<?php

/*

oop

*/

//引入db

require_once('db.php');

class Student

{

private $id;

private $name;

private $age;

public function __construct($id,$name,$age)

{

$this->id = $id;

$this->name = $name;

$this->age = $age;

}

public function getId(){return $this->id;}

public function getName(){return $this->name;}

public function getAge(){return $this->age;}

public function setName($name){$this->name = $name;}

public function setAge($age){$this->age = $age;}

}

class DBC

{

private $user;

private $pwd;

private $srv;

private $name;

private $result;

//构造

public function __construct($u,$p,$s,$n)

{

$this->user = $u;

$this->pwd = $p;

$this->srv = $s;

$this->name = $n;

}

public function op_db($sql)

{

$conn = mysql_connect($this->srv,$this->user,$this->pwd)

or die('error info:'.mysql_error());

mysql_select_db($this->name,$conn);

mysql_query('set names utf8');

$ret = mysql_query($sql,$conn);


$count = 0;

while($obj = mysql_fetch_row($ret))

{

$stu = new Student($obj[0],$obj[1],$obj[2]);

$this->result[$count] = $stu;

$count++;

}

//$this->result = mysql_fetch_array($ret);

}

public function getResult()

{

return $this->result;

}

}


$dbc = new DBC($db_user,$db_pwd,$db_srv,$db_name);

$dbc->op_db('select * from student');

$rs = $dbc->getResult();


//output student info

foreach($rs as $o)

{

echo '<tr>';

echo '<td>'.$o->getId().'</td>';

echo '<td>'.$o->getName().'</td>';

echo '<td>'.$o->getAge().'</td>';

echo '</tr>';

}

?>

</table>

</body>

</html>


/*

   SQL脚本:

-- phpMyAdmin SQL Dump

-- version 2.10.3

-- http://www.phpmyadmin.net

--

-- 主机: localhost

-- 生成日期: 2013 年 11 月 26 日 08:54

-- 服务器版本: 5.0.51

-- PHP 版本: 5.2.6


SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";


--

-- 数据库: `phpdb`

--


-- --------------------------------------------------------


--

-- 表的结构 `student`

--


CREATE TABLE `student` (

 `id` int(11) NOT NULL auto_increment,

 `name` varchar(30) NOT NULL,

 `age` int(11) NOT NULL,

 PRIMARY KEY  (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ;


--

-- 导出表中的数据 `student`

--


INSERT INTO `student` VALUES (1, 'codeguy', 23);

INSERT INTO `student` VALUES (2, 'join', 33);

INSERT INTO `student` VALUES (3, 'lucy', 18);

INSERT INTO `student` VALUES (4, 'jeff', 19);

INSERT INTO `student` VALUES (5, 'deff', 20);

INSERT INTO `student` VALUES (6, 'jerry', 21);

INSERT INTO `student` VALUES (7, 'tom', 22);

INSERT INTO `student` VALUES (9, '李白', 32);

INSERT INTO `student` VALUES (10, '李白', 32);

INSERT INTO `student` VALUES (11, '李白', 32);

INSERT INTO `student` VALUES (12, '李白', 32);

INSERT INTO `student` VALUES (13, '李白', 32);

INSERT INTO `student` VALUES (14, '李白', 32);

INSERT INTO `student` VALUES (15, '李白', 32);

*/