2020年6月2日

    1. <?php
    2. try{
    3. $dsn="mysql:host=127.0.0.1;port=3306;dbname=liuyan;charset=utf8";
    4. $pdo=new PDO($dsn,"root","root");
    5. //准备sql语句
    6. //$sql ="INSERT INTO books VALUES(2,'茄子',110)"; //增
    7. //$sql="DELETE FROM books WHERE book_id=1"; //删
    8. $sql="UPDATE books SET title='我起了一枪秒了'"; //改
    9. $res=$pdo->exec($sql);
    10. if($res){
    11. echo "操作成功" .$res;
    12. }else{
    13. echo "操作失败";
    14. }
    15. $sql="SELECT * FROM books";
    16. $res=$pdo->query($sql);
    17. if($res){
    18. echo"执行成功";
    19. $fetch_data[];
    20. while($row=$res->fetch(PDO::FETCH_ASSOC)){
    21. $fetch_data[]=$row;
    22. }
    23. var_dump($fetch_data);
    24. }else{
    25. echo "执行失败";
    26. }
    27. }catch(PDDException $e){
    28. var_dump($e->getMessage());
    29. }
    30. $sql="SELECT * FROM books";
    31. $res=$pdo->query($sql);
    32. if($res){
    33. echo"执行成功";
    34. $fetch_data[];
    35. while($row=$res->fetch(PDO::FETCH_ASSOC)){
    36. $fetch_data[]=$row;
    37. }
    38. var_dump($fetch_data);
    39. }else{
    40. echo "执行失败";
    41. }
    42. <?php
    43. try{
    44. $dsn="mysql:host=127.0.0.1;port=3306;dbname=liuyan;charset=utf8";
    45. $pdo=new PDO($dsn,"root","root");
    46. //准备sql语句
    47. //$sql ="INSERT INTO books VALUES(2,'茄子',110)"; //增
    48. //$sql="DELETE FROM books WHERE book_id=1"; //删
    49. $sql="UPDATE books SET title='我起了一枪秒了'"; //改
    50. $res=$pdo->exec($sql);
    51. if($res){
    52. echo "操作成功" .$res;
    53. }else{
    54. echo "操作失败";
    55. }
    56. $sql="SELECT * FROM books";
    57. $res=$pdo->query($sql);
    58. if($res){
    59. echo"执行成功";
    60. $fetch_data[];
    61. while($row=$res->fetch(PDO::FETCH_ASSOC)){
    62. $fetch_data[]=$row;
    63. }
    64. var_dump($fetch_data);
    65. }else{
    66. echo "执行失败";
    67. }
    68. }catch(PDDException $e){
    69. var_dump($e->getMessage());
    70. }
    71. $sql="SELECT * FROM books";
    72. $res=$pdo->query($sql);
    73. if($res){
    74. echo"执行成功";
    75. $fetch_data[];
    76. while($row=$res->fetch(PDO::FETCH_ASSOC)){
    77. $fetch_data[]=$row;
    78. }
    79. var_dump($fetch_data);
    80. }else{
    81. echo "执行失败";
    82. }
    83. <?php
    84. try{
    85. $dsn="mysql:host=127.0.0.1;port=3306;dbname=liuyan;charset=utf8";
    86. $pdo=new PDO($dsn,"root","root");
    87. //准备sql语句
    88. //$sql ="INSERT INTO books VALUES(2,'茄子',110)"; //增
    89. //$sql="DELETE FROM books WHERE book_id=1"; //删
    90. $sql="UPDATE books SET title='我起了一枪秒了'"; //改
    91. $res=$pdo->exec($sql);
    92. if($res){
    93. echo "操作成功" .$res;
    94. }else{
    95. echo "操作失败";
    96. }
    97. $sql="SELECT * FROM books";
    98. $res=$pdo->query($sql);
    99. if($res){
    100. echo"执行成功";
    101. $fetch_data[];
    102. while($row=$res->fetch(PDO::FETCH_ASSOC)){
    103. $fetch_data[]=$row;
    104. }
    105. var_dump($fetch_data);
    106. }else{
    107. echo "执行失败";
    108. }
    109. }catch(PDDException $e){
    110. var_dump($e->getMessage());
    111. }
    112. $sql="SELECT * FROM books";
    113. $res=$pdo->query($sql);
    114. if($res){
    115. echo"执行成功";
    116. $fetch_data[];
    117. while($row=$res->fetch(PDO::FETCH_ASSOC)){
    118. $fetch_data[]=$row;
    119. }
    120. var_dump($fetch_data);
    121. }else{
    122. echo "执行失败";
    123. }

    笔记
    $dsn结构
    每个结构用;分隔开
    1.数据源名(myself)
    2.安装地址(主机名)(host)
    3.端口(port)默认3306
    4.数据库名称(dbname)
    5.编码(charset) 常用UTF8

    PDO执行增删改操作
    步骤:
    1,准备sql语句
    2,执行sql语句
    当语句执行失败的时候回返回false;通常是语句写错了或者是表名写错了

    结果类别
    PDO::FETCH_ASSOC 结果设置未关联数组

    PDO::FETCH_BOTH 结果只为混合数组
    PDO::FETCH_NUM 结果只为索引数组