朱纯树博客
VPS测评推荐网站
cloudacead cloudacead

PHP7连接数据库的方法

  • PHP7中已经废除了mysql库了,则只能使用mysqli及PDO

mysqli面向对象风格

<?php
$serve = 'localhost:3306';
$username = 'root';
$password = 'admin123';
$dbname = 'examples';
$mysqli = new Mysqli($serve,$username,$password,$dbname);
if($mysqli->connect_error){
    die('connect error:'.$mysqli->connect_errno);
}
$mysqli->set_charset('UTF-8'); // 设置数据库字符集

$result = $mysqli->query('select * from customers');
$data = $result->fetch_all(); // 从结果集中获取所有数据
print_r($data);

 ?>

mysqli面向过程风格

<?php
$serve = 'localhost:3306';
$username = 'root';
$password = 'admin123';
$dbname = 'examples';
$link = mysqli_connect($serve,$username,$password,$dbname);
mysqli_set_charset($link,'UTF-8'); // 设置数据库字符集
$result = mysqli_query($link,'select * from customers');
$data = mysqli_fetch_all($result); // 从结果集中获取所有数据
print_r($data);

 ?>

PDO连接数据库

<?php
$serve = 'mysql:host=localhost:3306;dbname=examples;charset=utf8';
$username = 'root';
$password = 'admin123';

try{ // PDO连接数据库若错误则会抛出一个PDOException异常
    $PDO = new PDO($serve,$username,$password);
    $result = $PDO->query('select * from customers');
    $data = $result->fetchAll(PDO::FETCH_ASSOC); // PDO::FETCH_ASSOC表示将对应结果集中的每一行作为一个由列名索引的数组返回
    print_r($data);
} catch (PDOException $error){
    echo 'connect failed:'.$error->getMessage();
}

 ?>

使用PDO或mysqli都可以连接mysql,但更推荐使用PDO连接数据库,因为PDO支持12种不同的数据库驱动程序,mysqli只支持mysql,而且PDO性能更高

赞(0) 打赏
未经允许不得转载:VPS测评推荐网站 - 朱纯树博客 » PHP7连接数据库的方法

评论 14

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #0

    wonderful points altogether, you simply gained a emblem new reader.
    What would you suggest about your submit that you just made some days in the
    past? Any sure?

    tinyurl.com4周前 (06-11)回复
  2. #0

    Wow that was odd. I just wrote an incredibly long comment but after I clicked submit
    my comment didn’t appear. Grrrr… well I’m not writing
    all that over again. Anyways, just wanted to say great blog!

    tinyurl.com1个月前 (06-06)回复
  3. #0

    Thank you for the good writeup. It actually was a enjoyment account it.
    Glance complicated to far added agreeable from you!

    However, how can we be in contact?

  4. #0

    I think this is among the most vital info for me. And i am glad reading your article.

    But wanna remark on some general things, The website style is great, the articles is really excellent
    : D. Good job, cheers

    bit.ly1个月前 (06-05)回复
  5. #0

    I blog often and I seriously appreciate your information.
    The article has really peaked my interest. I’m going
    to book mark your site and keep checking for
    new information about once a week. I opted in for your RSS
    feed too.

    tinyurl.com2个月前 (05-16)回复
  6. #0

    Thank you for sharing your info. I truly appreciate your efforts and I am
    waiting for your next write ups thank you once again.

    tinyurl.com2个月前 (05-11)回复
  7. #0

    You need to be a part of a contest for one of the finest sites on the web.
    I will highly recommend this web site!

    tinyurl.com2个月前 (05-10)回复
  8. #0

    Appreciate the recommendation. Will try it out.

    gamefly3个月前 (04-10)回复
  9. #0

    I always emailed this weblog post page to all my associates, since if like to read it then my links will too.

    flight tickets cheap3个月前 (04-06)回复
  10. #0

    Wonderful article! This is the type of info that should be shared around the web.

    Shame on Google for now not positioning this publish higher!
    Come on over and discuss with my site . Thanks =)

    flight tickets cheap3个月前 (04-05)回复
  11. #0

    It’s actually very complicated in this full of activity life
    to listen news on Television, therefore I simply use web for that reason, and
    take the most up-to-date news.

    cheap flights now3个月前 (04-04)回复
  12. #0

    Greetings! Very useful advice in this particular post! It’s the little changes that will make the most important changes.
    Thanks for sharing!

    ticket flight3个月前 (04-04)回复
  13. #0

    What a data of un-ambiguity and preserveness of precious
    knowledge about unpredicted feelings.

    cheap tickets flights3个月前 (04-03)回复
  14. #0