- <?php
- dd = new LampDb('localhost','root','2184237','note','utf8');
- $sql = "select * from mynote";
- $handle = $dd->query($sql);
- $aa = $dd->fetchOneRow($handle);
- print_r($aa);
- echo "<hr />";
- $ddd = $dd->fetchRows($handle);
- for ($n=0; $n < count($ddd); $n++){
- echo $ddd[$n]['id'] . "标题" . $ddd[$n]['subject'];
- echo "<br />";
- }
- echo "<hr />";
- echo "nums=" . $dd->fetchRowNums($handle);
- ?>
- php
- class LampDb
- {
- private $conn;
- //连接数据库
- function __construct($host,$user,$pass,$dbname,$charset)
- {
- $this->conn = mysql_connect($host,$user,$pass);
- mysql_query('set names $charset');
- $db = mysql_select_db($dbname,$this->conn);
- }
- //查询语句
- function query($sql)
- {
- $handle = mysql_query($sql,$this->conn);
- return $handle;
- }
- //查询一条记录
- function fetchOneRow($handle)
- {
- $aa = mysql_fetch_assoc($handle);
- return $aa;
- }
- //查询多条记录
- function fetchRows($handle,$i=0)
- {
- while($bb = mysql_fetch_assoc($handle)){
- $bbb[$i] = $bb;
- $i++;
- }
- return $bbb;
- }
- //获取一个查询语句返回的记录数
- function fetchRowNums($handle)
- {
- //$handle = $this->query($sql);
- $num = mysql_num_rows($handle);
- return $num;
- }
- }
- ?>
时间: 2024-10-03 14:46:21