2020年6月4日

    1. <?php
    2. try{
    3. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
    4. $pdo=new PDO($dsn,"root","root");
    5. $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
    6. $sql="SELECT * FROM a";
    7. $zhi=$pdo->query($sql);
    8. if($zhi){
    9. echo "执行成功";
    10. echo "<pre>";
    11. $fetch_data=[];
    12. while ($row=$zhi->fetch(PDO::FETCH_ASSOC)){
    13. $fetch_data[] = $row;
    14. }
    15. var_dump($fetch_data);
    16. echo"</pre>";
    17. }else{
    18. echo"执行失败";
    19. }
    20. }catch(PDOException $e){
    21. var_dump($e->getMessage());
    22. }
    23. //设置错误报告
    24. try{
    25. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
    26. $pdo=new PDO($dsn,"root","root");
    27. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    28. $sql="SELECT * FROM a";
    29. $zhi=$pdo->query($sql);
    30. if($zhi){
    31. echo"执行成功";
    32. }else{
    33. var_dump($pdo->errorInfo()[2]);
    34. }
    35. }catch(PDOException $e){
    36. var_dump($e->getMessage());
    37. }
    38. try{
    39. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
    40. $pdo=new PDO($dsn,"root","root");
    41. //余额表
    42. //CREATE TABLE a(
    43. // n VARCHAR(15) NOT NULL,
    44. // r INT(10)
    45. // )ENGINE=innodb;
    46. //INSERT INTO a VALUES('夏亚','1000'),('阿姆罗','1000');
    47. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    48. $pdo->beginTransaction();
    49. $sql="UPDATE a SET rmb=rmb-200 WHERE name='夏亚'";
    50. $pdo->exec($sql);
    51. $sql="UPDATE a SET rmb=rmb+200 WHERE name='阿姆罗'";
    52. $pdo->exec($sql);
    53. $pdo->commit();
    54. }catch(PDOException $e){
    55. var_dump($e->getMessage());
    56. }
    57. //预处理流程
    58. try{
    59. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
    60. $pdo=new PDO($dsn,"root","root");
    61. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    62. $sql="SELECT * FROM a";
    63. $zhi=$pdo->prepare($sql);
    64. $res=$zhi->execute();
    65. if($res){
    66. $tl=$zhi->fetchAll(PDO::FETCH_ASSOC);
    67. var_dump($tl);
    68. }else{
    69. echo"执行失败";
    70. }
    71. }catch(PDOException $e){
    72. $pdo->rollback();
    73. var_dump($e->getMessage());
    74. }
    75. try{
    76. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
    77. $pdo=new PDO($dsn,"root","root");
    78. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    79. $sql="SELECT * FROM a WHERE name LIKE ? AND rmb=?;";
    80. $zhi=$pdo->prepare($sql);
    81. $s="%红%";
    82. $r="1000";
    83. $zhi->bindParam(1,$s);
    84. $zhi->bindParam(2,$r);
    85. $zhi->bindValue(1,"%子%");
    86. $zhi->bindValue(2,3200);
    87. $res=$zhi->execute();
    88. if($res){
    89. var_dump($zhi->fetchAll(2));
    90. }
    91. }catch(PDOException $e){
    92. $pdo->rollback();
    93. var_dump($e->getMessage());
    94. }
    95. try{
    96. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
    97. $pdo=new PDO($dsn,"root","root");
    98. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    99. $sql="SELECT * FROM a WHERE name LIKE :search AAND rmb = :qian;";
    100. $zhi=$pdo->prepare($sql);
    101. $s="%子%";
    102. $qian=3200;
    103. $zhi->bindParam('s',$s);
    104. $zhi->bindValue('qian',3200);
    105. $res=$zhi->execute();
    106. if($res){
    107. var_dump($zhi->fetchAll(2));
    108. }catch(PDOException $e){
    109. var_dump($e->getMessage());
    110. }
    111. <?php
    112. try{
    113. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
    114. $pdo=new PDO($dsn,"root","root");
    115. $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
    116. $sql="SELECT * FROM a";
    117. $zhi=$pdo->query($sql);
    118. if($zhi){
    119. echo "执行成功";
    120. echo "<pre>";
    121. $fetch_data=[];
    122. while ($row=$zhi->fetch(PDO::FETCH_ASSOC)){
    123. $fetch_data[] = $row;
    124. }
    125. var_dump($fetch_data);
    126. echo"</pre>";
    127. }else{
    128. echo"执行失败";
    129. }
    130. }catch(PDOException $e){
    131. var_dump($e->getMessage());
    132. }
    133. //设置错误报告
    134. try{
    135. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
    136. $pdo=new PDO($dsn,"root","root");
    137. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    138. $sql="SELECT * FROM a";
    139. $zhi=$pdo->query($sql);
    140. if($zhi){
    141. echo"执行成功";
    142. }else{
    143. var_dump($pdo->errorInfo()[2]);
    144. }
    145. }catch(PDOException $e){
    146. var_dump($e->getMessage());
    147. }
    148. try{
    149. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
    150. $pdo=new PDO($dsn,"root","root");
    151. //余额表
    152. //CREATE TABLE a(
    153. // n VARCHAR(15) NOT NULL,
    154. // r INT(10)
    155. // )ENGINE=innodb;
    156. //INSERT INTO a VALUES('夏亚','1000'),('阿姆罗','1000');
    157. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    158. $pdo->beginTransaction();
    159. $sql="UPDATE a SET rmb=rmb-200 WHERE name='夏亚'";
    160. $pdo->exec($sql);
    161. $sql="UPDATE a SET rmb=rmb+200 WHERE name='阿姆罗'";
    162. $pdo->exec($sql);
    163. $pdo->commit();
    164. }catch(PDOException $e){
    165. var_dump($e->getMessage());
    166. }
    167. //预处理流程
    168. try{
    169. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
    170. $pdo=new PDO($dsn,"root","root");
    171. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    172. $sql="SELECT * FROM a";
    173. $zhi=$pdo->prepare($sql);
    174. $res=$zhi->execute();
    175. if($res){
    176. $tl=$zhi->fetchAll(PDO::FETCH_ASSOC);
    177. var_dump($tl);
    178. }else{
    179. echo"执行失败";
    180. }
    181. }catch(PDOException $e){
    182. $pdo->rollback();
    183. var_dump($e->getMessage());
    184. }
    185. try{
    186. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
    187. $pdo=new PDO($dsn,"root","root");
    188. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    189. $sql="SELECT * FROM a WHERE name LIKE ? AND rmb=?;";
    190. $zhi=$pdo->prepare($sql);
    191. $s="%红%";
    192. $r="1000";
    193. $zhi->bindParam(1,$s);
    194. $zhi->bindParam(2,$r);
    195. $zhi->bindValue(1,"%子%");
    196. $zhi->bindValue(2,3200);
    197. $res=$zhi->execute();
    198. if($res){
    199. var_dump($zhi->fetchAll(2));
    200. }
    201. }catch(PDOException $e){
    202. $pdo->rollback();
    203. var_dump($e->getMessage());
    204. }
    205. try{
    206. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
    207. $pdo=new PDO($dsn,"root","root");
    208. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    209. $sql="SELECT * FROM a WHERE name LIKE :search AAND rmb = :qian;";
    210. $zhi=$pdo->prepare($sql);
    211. $s="%子%";
    212. $qian=3200;
    213. $zhi->bindParam('s',$s);
    214. $zhi->bindValue('qian',3200);
    215. $res=$zhi->execute();
    216. if($res){
    217. var_dump($zhi->fetchAll(2));
    218. }catch(PDOException $e){
    219. var_dump($e->getMessage());
    220. }
    221. <?php
    222. try{
    223. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
    224. $pdo=new PDO($dsn,"root","root");
    225. $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);
    226. $sql="SELECT * FROM a";
    227. $zhi=$pdo->query($sql);
    228. if($zhi){
    229. echo "执行成功";
    230. echo "<pre>";
    231. $fetch_data=[];
    232. while ($row=$zhi->fetch(PDO::FETCH_ASSOC)){
    233. $fetch_data[] = $row;
    234. }
    235. var_dump($fetch_data);
    236. echo"</pre>";
    237. }else{
    238. echo"执行失败";
    239. }
    240. }catch(PDOException $e){
    241. var_dump($e->getMessage());
    242. }
    243. //设置错误报告
    244. try{
    245. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
    246. $pdo=new PDO($dsn,"root","root");
    247. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    248. $sql="SELECT * FROM a";
    249. $zhi=$pdo->query($sql);
    250. if($zhi){
    251. echo"执行成功";
    252. }else{
    253. var_dump($pdo->errorInfo()[2]);
    254. }
    255. }catch(PDOException $e){
    256. var_dump($e->getMessage());
    257. }
    258. try{
    259. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo;charset=utf8";
    260. $pdo=new PDO($dsn,"root","root");
    261. //余额表
    262. //CREATE TABLE a(
    263. // n VARCHAR(15) NOT NULL,
    264. // r INT(10)
    265. // )ENGINE=innodb;
    266. //INSERT INTO a VALUES('夏亚','1000'),('阿姆罗','1000');
    267. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    268. $pdo->beginTransaction();
    269. $sql="UPDATE a SET rmb=rmb-200 WHERE name='夏亚'";
    270. $pdo->exec($sql);
    271. $sql="UPDATE a SET rmb=rmb+200 WHERE name='阿姆罗'";
    272. $pdo->exec($sql);
    273. $pdo->commit();
    274. }catch(PDOException $e){
    275. var_dump($e->getMessage());
    276. }
    277. //预处理流程
    278. try{
    279. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
    280. $pdo=new PDO($dsn,"root","root");
    281. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    282. $sql="SELECT * FROM a";
    283. $zhi=$pdo->prepare($sql);
    284. $res=$zhi->execute();
    285. if($res){
    286. $tl=$zhi->fetchAll(PDO::FETCH_ASSOC);
    287. var_dump($tl);
    288. }else{
    289. echo"执行失败";
    290. }
    291. }catch(PDOException $e){
    292. $pdo->rollback();
    293. var_dump($e->getMessage());
    294. }
    295. try{
    296. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
    297. $pdo=new PDO($dsn,"root","root");
    298. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    299. $sql="SELECT * FROM a WHERE name LIKE ? AND rmb=?;";
    300. $zhi=$pdo->prepare($sql);
    301. $s="%红%";
    302. $r="1000";
    303. $zhi->bindParam(1,$s);
    304. $zhi->bindParam(2,$r);
    305. $zhi->bindValue(1,"%子%");
    306. $zhi->bindValue(2,3200);
    307. $res=$zhi->execute();
    308. if($res){
    309. var_dump($zhi->fetchAll(2));
    310. }
    311. }catch(PDOException $e){
    312. $pdo->rollback();
    313. var_dump($e->getMessage());
    314. }
    315. try{
    316. $dsn="mysql:host=127.0.0.1;port=3306;dbname=demo1;charset=utf8";
    317. $pdo=new PDO($dsn,"root","root");
    318. $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    319. $sql="SELECT * FROM a WHERE name LIKE :search AAND rmb = :qian;";
    320. $zhi=$pdo->prepare($sql);
    321. $s="%子%";
    322. $qian=3200;
    323. $zhi->bindParam('s',$s);
    324. $zhi->bindValue('qian',3200);
    325. $res=$zhi->execute();
    326. if($res){
    327. var_dump($zhi->fetchAll(2));
    328. }catch(PDOException $e){
    329. var_dump($e->getMessage());
    330. }

    笔记:
    设置PDO的选项:setAttribute()

    设置列名强制大写:pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER);
    数据库里面设置的字段是大写php获取到就是大写pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_NATURAL);
    设置强制列名小写:pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_LOWER);

    结果类别
    PDO::FETCH_ASSOC 结果设置未关联数组
    PDO::FETCH_BOTH 结果只为混合数组
    PDO::FETCH_NUM 结果只为索引数组
    fetch(结果类别);获取一条数据;类似于mysql的写法

    事物处理:事物处理是用来保证数据操作的原子性(成功或失败)
    保证多个sql语句处理全部成功则数据修改生效,如果其中任何一个语句出差那所有的语句修改都不生效

    事物处理对数据存储引擎有要求
    MyISAM 不支持事物

    innodb 支持事物

    PDO预处理 预先处理数据步骤:
    1.发送sql语句给预处理2.执行预处理的sql语句

    参数绑定
    sql语句在开发中,比如搜索功能;通常搜索值都是由外传递进来的;
    sql注入:如果用户是黑客,输入一些特殊的代码,服务器如果没处理直接丢到sql语句发送给mysql执行可能会被黑掉