计应134(实验班) 幸南霖
当我开始去接触PHP的时候,真切的感受到其所具有的魅力,本着学习的态度和打破固有的语言和模式的想法,开始了MySQL之旅。
1.mysql扩展
<?php
//1:获取数据库连接$connection = @ mysql_connect(‘127.0.0.1:3306‘, ‘root‘, ‘root‘) or die(‘Mysql connection failed ‘ . mysql_error());//2:选择数据库mysql_select_db(‘phptest‘) or die(‘Database phptest not exist ‘ . mysql_error());//3:查询数据$sql = ‘SELECT id as `编号`, name as `姓名`, birthday as `出生日期`, phone as `联系方式` , address as `地址` ‘ .‘FROM `t_user` LIMIT 0 , 30‘;$result = mysql_query($sql) or die(‘Query failed ‘ . mysql_error());//4:处理查询结果#打印查询数据echo ‘<table>‘;#打印列信息$i = 0;echo ‘<tr>‘;while ($i < mysql_num_fields($result)) { $meta = mysql_fetch_field($result); echo ‘<td>‘; echo $meta->name; echo ‘</td>‘; $i++;}echo ‘<tr/>‘;#打印行记录while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { echo ‘<tr>‘; foreach ($line as $value) { echo ‘<td>‘ . $value . ‘</td>‘; } echo ‘</tr>‘;}echo ‘</table>‘;//5:释放结果内存mysql_free_result($result);//6:关闭数据库连接mysql_close($connection);?><?php
#使用面向对象的方式操作mysql数据库
//1:创建mysqli对象
$mysqli= newmysqli();
//2:连接数据库
$mysqli->connect(‘127.0.0.1‘, ‘root‘, ‘root‘);
$mysqli->select_db(‘phptest‘);
if($mysqli->connect_errno) {
echo‘连接数据库失败‘;
exit();
}
//3:获取MySQLi_STMT(执行SQL命令)对象,$stmt
$sql= ‘SELECT id as `编号`, name as `姓名`, birthday as `出生日期`, phone as `联系方式` , address as `地址` ‘.
‘FROM `t_user` LIMIT 0 , 30‘;
if($stmt= $mysqli->prepare($sql)) {
#执行查询
$stmt->execute();
$stmt->bind_result($id, $name, $birthday, $phone, $address);
#打印查询数据
echo‘<table>‘;
#打印列信息
echo‘<tr>‘;
$meta= $stmt->result_metadata()->fetch_fields();
foreach($metaas$val) {
echo‘<td>‘;
echo$val->name;
echo‘</td>‘;
}
echo‘<tr/>‘;
while($stmt->fetch()) {
echo‘<tr>‘;
echo‘<td>‘. $id. ‘</td>‘;
echo‘<td>‘. $name. ‘</td>‘;
echo‘<td>‘. $birthday. ‘</td>‘;
echo‘<td>‘. $phone. ‘</td>‘;
echo‘<td>‘. $address. ‘</td>‘;
echo‘</tr>‘;
}
echo‘</table>‘;
echo‘<h4>查询结果记录数:‘. $stmt->num_rows.‘</h4>‘;
//4:关闭MySQLi_STMT
$stmt->close();
}
//5:关闭数据库连接
$mysqli->close();
?>
3.PDO_MYSQL扩展
数据库连接配置信息pdo-inc.php:
<?php $dsn = ‘mysql:host=127.0.0.1:3306;dbname=phptest‘;$username = ‘root‘;$password = ‘root‘;$opt = array ( PDO :: ATTR_TIMEOUT => 40);?><?php//1:引入数据库连接信息require_once (‘pdo-inc.php‘);//2:创建PDO对象try { $pdo = new PDO($dsn, $username, $password, $opt);} catch (PDOException $e) { echo ‘数据库连接失败 ‘ . $e->getMessage(); exit ();}$sql = ‘SELECT id as `编号`, name as `姓名`, birthday as `出生日期`, phone as `联系方式` , address as `地址` ‘ .‘FROM `t_user` LIMIT 0 , 30‘;//3:创建预编译命令对象PDOStatementif ($stmt = $pdo->prepare($sql)) { //4:执行 $stmt->execute(); #打印查询数据 echo ‘<table>‘; echo ‘<tr>‘; for ($i = 0, $columnCount = $stmt->columnCount(); $i < $columnCount; $i++) { $meta = $stmt->getColumnMeta($i); echo ‘<td>‘; echo $meta[‘name‘]; echo ‘</td>‘; } echo ‘<tr/>‘; $row_num = 0; while ($row = $stmt->fetch(PDO :: FETCH_NUM)) { $row_num++; echo ‘<tr>‘; foreach ($row as $value) { echo ‘<td>‘ . $value . ‘</td>‘; } echo ‘</tr>‘; } echo ‘</table>‘; #需要统计查询的行数 echo ‘一共‘ . $row_num . ‘条记录<br/>使用PDOStatement::rowCount来统计,一共‘ . $stmt->rowCount() . ‘条记录‘; //5:关闭游标 $stmt->closeCursor();}//6:释放PDO对象$pdo = null;?>原文:http://www.cnblogs.com/9527leo/p/4912309.html