PHP操作MySQL数据库的几种方式

计应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 );
?>
 2.mysqli扩展

<?php

#使用面向对象的方式操作mysql数据库

//1:创建mysqli对象

$mysqlinewmysqli();

//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
);
 
?>
使用PDO访问数据库:
<?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:创建预编译命令对象PDOStatement
if  ( $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;
?>

转载于:https://www.cnblogs.com/9527leo/p/4912309.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值