Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.37 KB | None | 0 0
  1. <?php
  2. $dbh = new
  3. PDO('mysql:host=localhost;dbname=test', $user, $pass);
  4. ?>
  5. <?php
  6. try {
  7. $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
  8. foreach
  9. ($dbh
  10. -
  11. >query('SELECT * from FOO') as $row) {
  12. print_r($row);
  13. }
  14. $dbh = null;
  15. } catch (PDOException $e) {
  16. print "Error!: " . $e
  17. -
  18. >getMessage() . "<br/>";
  19. die();
  20. }
  21. ?>
  22. <?php
  23. $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
  24. // use the connection here
  25. $sth = $dbh
  26. -
  27. >query('SELECT * FROM foo');
  28. // and now we're done; close it
  29. $sth = null;
  30. $dbh = null;
  31. ?>
  32. <?php
  33. $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
  34. PDO::ATTR
  35. _PERSISTENT => true
  36. ));
  37. ?>
  38. <?php
  39. try {
  40. $dbh = new PDO('mysql:host=localhost;dbname=test', 'user', 'password',
  41. array(PDO::ATTR_PERSISTENT => true));
  42. echo "Connected
  43. \
  44. n";
  45. } catch (Exception $e) {
  46. die("Unable to connect: " . $e
  47. -
  48. >getMessage());
  49. }
  50. try {
  51. $dbh
  52. -
  53. >setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  54. $dbh
  55. -
  56. >beginTransaction();
  57. $dbh
  58. -
  59. >exec("INSERT INTO dept VALUES ('50','TEST','WASHNGTON')");
  60. $dbh
  61. -
  62. >exec("INSERT INTO emp VALUES ('7944','JOHN','CLERK','7782','1983
  63. -
  64. 01
  65. -
  66. 01','1000.00',NULL,
  67. '50')");
  68. $dbh
  69. -
  70. >commit();
  71. } catch (Exception $e) {
  72. $dbh
  73. -
  74. >rollBack();
  75. echo "Failed: " . $e
  76. -
  77. >getMessage();
  78. }
  79. ?>
  80. <?php
  81. $stmt = $dbh
  82. -
  83. >prepare("INSERT INTO REGISTRY (name, value) VALUES (:name,
  84. :value)");
  85. $stmt
  86. -
  87. >bindParam(':name', $name);
  88. $stmt
  89. -
  90. >bindParam(':va
  91. lue', $value);
  92. // insert one row
  93. $name = 'one';
  94. $value = 1;
  95. $stmt
  96. -
  97. >execute();
  98. // insert another row with different values
  99. $name = 'two';
  100. $value = 2;
  101. $stmt
  102. -
  103. >execute();
  104. ?>
  105. <?php
  106. $stmt = $dbh
  107. -
  108. >prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
  109. $stmt
  110. -
  111. >bindParam(1, $name);
  112. $stmt
  113. -
  114. >bindParam(2, $value);
  115. //
  116. insert one row
  117. $name = 'one';
  118. $value = 1;
  119. $stmt
  120. -
  121. >execute();
  122. // insert another row with different values
  123. $name = 'two';
  124. $value = 2;
  125. $stmt
  126. -
  127. >execute();
  128. ?>
  129. <?php
  130. $stmt = $dbh
  131. -
  132. >prepare("SELECT * FROM REGISTRY where name = ?")
  133. ;
  134. if ($stmt
  135. -
  136. >execute(array($_GET['name']))) {
  137. while ($row = $stmt
  138. -
  139. >fetch()) {
  140. print_r($row);
  141. }
  142. }
  143. ?>
  144. <?php
  145. $stmt = $dbh
  146. -
  147. >prepare("CALL sp_returns_string(?)");
  148. $stmt
  149. -
  150. >bindParam(1, $return_value, PDO::PARAM_ST
  151. R, 4000);
  152. // call the stored procedure
  153. $stmt
  154. -
  155. >execute();
  156. print "procedure returned $return_value
  157. \
  158. n";
  159. ?>
  160. <?php
  161. $stmt = $dbh
  162. -
  163. >prepare("CALL sp_take
  164. s_string_returns_string(?)");
  165. $value = 'hello';
  166. $stmt
  167. -
  168. >bindParam(1, $value, PDO::PARAM_STR|PDO::PARAM_INPUT_OUTPUT, 4000);
  169. // call the stored procedure
  170. $stmt
  171. -
  172. >execute();
  173. print "procedure returned $value
  174. \
  175. n";
  176. ?>
  177. <?php
  178. $stmt = $dbh
  179. -
  180. >prepare("SELECT * FROM REGISTRY where name LIKE '%?%'");
  181. $stmt
  182. -
  183. >execute(array($_GET['name']));
  184. // placeholder must be used in the place of the whole value
  185. $stmt = $dbh
  186. -
  187. >prepare("SELECT * FROM REGISTRY where name LIKE ?");
  188. $s
  189. tmt
  190. -
  191. >execute(array("%$_GET[
  192. '
  193. name
  194. '
  195. ]%"));
  196. ?><?php
  197. $stmt = $dbh
  198. -
  199. >prepare("SELECT * FROM REGISTRY where name LIKE '%?%'");
  200. $stmt
  201. -
  202. >execute(array($_GET['name']));
  203. // placeholder must be used in the place of the whole value
  204. $stmt = $dbh
  205. -
  206. >prepare("SELECT * FROM REGISTRY where name LIKE ?");
  207. $s
  208. tmt
  209. -
  210. >execute(array("%$_GET[
  211. '
  212. name
  213. '
  214. ]%"));
  215. ?>
  216. <?php
  217. $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
  218. $user = 'dbuser';
  219. $password = 'dbpass';
  220. try {
  221. $dbh = new PDO($dsn, $user, $password);
  222. $dbh
  223. -
  224. >setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  225. } catch (PDOException $e) {
  226. echo 'Connection failed: ' . $e
  227. -
  228. >getMessage();
  229. }
  230. ?>
  231. <?php
  232. $dsn = 'mysql:dbname=test;host=127.0.0.1';
  233. $user = 'googl
  234. eguy';
  235. $password = 'googleguy';
  236. /*
  237. Using try/catch around the constructor is still valid even though we set the
  238. ERRMODE to WARNING since
  239. PDO::__construct will always throw a PDOException if the connection fails.
  240. */
  241. try {
  242. $dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE =>
  243. PDO::ERRMODE_WARNING));
  244. } catch (PDOException $e) {
  245. echo 'Connection failed: ' . $e
  246. -
  247. >getMessage();
  248. exit;
  249. }
  250. // This will cause PDO to throw an error of level E_WARNING instead of
  251. an
  252. exception (when the table doesn't exist)
  253. $dbh
  254. -
  255. >query("SELECT wrongcolumn FROM wrongtable");
  256. ?>
  257. <?php
  258. $db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
  259. $stmt = $db
  260. -
  261. >prepare("select contenttype, imagedata from images where id=?");
  262. $stmt
  263. -
  264. >execute(array($_GET['id']));
  265. $stmt
  266. -
  267. >bindColumn(1, $type, PDO::PARAM_STR, 256);
  268. $stmt
  269. -
  270. >bindColumn(2, $lob, PDO
  271. ::PARAM_LOB);
  272. $stmt
  273. -
  274. >fetch(PDO::FETCH_BOUND);
  275. header("Content
  276. -
  277. Type: $type");
  278. fpassthru($lob);
  279. ?>
  280. <?php
  281. $db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
  282. $stmt = $db
  283. -
  284. >prepare("insert into images (id, contenttype, imagedata) values (?,
  285. ?, ?)");
  286. $id = g
  287. et_new_id(); // some function to allocate a new ID
  288. // assume that we are running as part of a file upload form
  289. // You can find more information in the PHP documentation
  290. $fp = fopen($_FILES['file']['tmp_name'], 'rb');
  291. $stmt
  292. -
  293. >bindParam(1, $id);
  294. $stmt
  295. -
  296. >bin
  297. dParam(2, $_FILES['file']['type']);
  298. $stmt
  299. -
  300. >bindParam(3, $fp, PDO::PARAM_LOB);
  301. $db
  302. -
  303. >beginTransaction();
  304. $stmt
  305. -
  306. >execute();
  307. $db
  308. -
  309. >commit();
  310. ?>
  311. <?php
  312. $db = new PDO('oci:', 'scott', 'tiger');
  313. $stmt = $db
  314. -
  315. >prepare("insert into images (id, contenttype, imagedata) " .
  316. "VALUES (?, ?, EMPTY_BLOB()) RETURNING imagedata INTO ?");
  317. $id = get_new_id(); // some function to allocate a new ID
  318. // assume that we are running as part of a file upload form
  319. // You can find more information in the PHP documentation
  320. $fp = fopen($_FILES
  321. ['file']['tmp_name'], 'rb');
  322. $stmt
  323. -
  324. >bindParam(1, $id);
  325. $stmt
  326. -
  327. >bindParam(2, $_FILES['file']['type']);
  328. $stmt
  329. -
  330. >bindParam(3, $fp, PDO::PARAM_LOB);
  331. $db
  332. -
  333. >beginTransaction();
  334. $stmt
  335. -
  336. >execute();
  337. $db
  338. -
  339. >commit();
  340. ?>
  341. <?php
  342. /* Begin a transaction, turning off autocommit */
  343. $dbh
  344. -
  345. >beginTransaction();
  346. /* Change the database schema and data */
  347. $sth = $dbh
  348. -
  349. >exec("DROP TABLE fruit");
  350. $sth = $dbh
  351. -
  352. >exec("UPDATE dessert
  353. SET name = 'hamburger'");
  354. /* Recognize
  355. mistake and roll back changes */
  356. $dbh
  357. -
  358. >rollBack();
  359. /* Database connection is now back in autocommit mode */
  360. ?>
  361. <?php
  362. /* Begin a transaction, turning off
  363. autocommit */
  364. $dbh
  365. -
  366. >beginTransaction();
  367. /* Insert multiple records on an all
  368. -
  369. or
  370. -
  371. nothing basis */
  372. $sql = 'INSERT INTO fruit
  373. (name, colour, calories)
  374. VALUES (?, ?, ?)';
  375. $sth = $dbh
  376. -
  377. >prepare($sql);
  378. foreach ($fruits as $fruit) {
  379. $sth
  380. -
  381. >execute(array(
  382. $fruit
  383. -
  384. >name,
  385. $fruit
  386. -
  387. >colour,
  388. $fruit
  389. -
  390. >calories,
  391. ));
  392. }
  393. /* Commit the changes */
  394. $dbh
  395. -
  396. >commit();
  397. /* Database connection is now back in autocommit mode */
  398. ?>
  399. <?php
  400. /* Begin a transaction, turning off autocommit */
  401. $dbh
  402. -
  403. >beginTransaction();
  404. /* Change the database schema */
  405. $sth = $dbh
  406. -
  407. >exec("DROP TABLE fruit");
  408. /* Commit the changes */
  409. $dbh
  410. -
  411. >commit();
  412. /* Database connection is now back in autocommit mode */
  413. ?>
  414. <?php
  415. /* Connect to a MySQL database using driver invocation */
  416. $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
  417. $user = 'dbuser';
  418. $password = 'dbpass';
  419. try {
  420. $dbh = new P
  421. DO($dsn, $user, $password);
  422. } catch (PDOException $e) {
  423. echo 'Connection failed: ' . $e
  424. -
  425. >getMessage();
  426. }
  427. ?>
  428. <?php
  429. /* Provoke an error
  430. --
  431. the BONES table does not exist */
  432. $dbh
  433. -
  434. >exec("INSERT INTO bones(skull) VALUES ('lucy')");
  435. echo "
  436. \
  437. nPDO::errorCode(): ", $dbh
  438. -
  439. >errorCode();
  440. ?>
  441. <?php
  442. /* Provoke an error
  443. --
  444. bogus SQL syntax */
  445. $stmt = $dbh
  446. -
  447. >prepare('bogus sql');
  448. if (!$stmt) {
  449. echo "
  450. \
  451. nPDO::errorInfo():
  452. \
  453. n";
  454. print_r($dbh
  455. -
  456. >errorInfo());
  457. }
  458. ?>
  459. <?php
  460. print_r(PDO::getAvailableDrivers());
  461. ?>
  462. <?php
  463. $conn = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');
  464. $attributes = array(
  465. "AUTOCOMMIT", "ERRMODE", "CASE", "CLIENT_VERSION", "CONNECTION_STATUS",
  466. "ORACLE_NULLS", "PERSISTENT", "PREFETCH", "SERVER_INFO", "SERVER_VERSION",
  467. "TIMEOUT"
  468. );
  469. foreach ($attributes as $val) {
  470. echo "PDO::ATTR_$val: ";
  471. echo $conn
  472. -
  473. >getAtt
  474. ribute(constant("PDO::ATTR_$val")) . "
  475. \
  476. n";
  477. }
  478. ?>
  479. <?php
  480. $conn = new PDO('sqlite:/home/lynn/music.sql3');
  481. /* Simple string */
  482. $string = 'Nice';
  483. print "Unquoted string: $string
  484. \
  485. n";
  486. print "Quoted string: " . $conn
  487. -
  488. >quote($string) . "
  489. \
  490. n";
  491. ?>
  492. <?php
  493. $conn = new PDO('sqlite:/home/lynn/music.sql3');
  494. /* Dangerous string */
  495. $string = 'Naughty
  496. \
  497. ' string';
  498. print "Unquoted string: $string
  499. \
  500. n";
  501. print "Quoted string:" . $conn
  502. -
  503. >quote($string) . "
  504. \
  505. n";
  506. ?>
  507. <?php
  508. function getFruit($conn) {
  509. $sql = 'SELECT name, color, calories FROM fruit ORDER BY name';
  510. foreach ($conn
  511. -
  512. >query($sql) as $row) {
  513. print $row['name'] . "
  514. \
  515. t";
  516. print $row['color'] . "
  517. \
  518. t";
  519. print $row['calories'] . "
  520. \
  521. n";
  522. }
  523. }
  524. ?>
  525. <?php
  526. /* Execute a prepared statement by passing an array of values */
  527. $sql = 'SELECT name, colour, calories
  528. FROM fruit
  529. WHERE calories < :calories AND colour = :colour';
  530. $sth = $dbh
  531. -
  532. >prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  533. $sth
  534. -
  535. >execute(array(':calories' => 150, ':colour' => 'red'));
  536. $red = $sth
  537. -
  538. >fetchAll();
  539. $sth
  540. -
  541. >execute(array(':calories' => 175,
  542. ':colour' => 'yellow'));
  543. $yellow = $sth
  544. -
  545. >fetchAll();
  546. ?>
  547. <?php
  548. /* Execute a prepared statement by passing an array of values */
  549. $sth = $dbh
  550. -
  551. >prepare('SELECT name, colour, calories
  552. FROM fruit
  553. WHERE calories < ? AND colo
  554. ur = ?');
  555. $sth
  556. -
  557. >execute(array(150, 'red'));
  558. $red = $sth
  559. -
  560. >fetchAll();
  561. $sth
  562. -
  563. >execute(array(175, 'yellow'));
  564. $yellow = $sth
  565. -
  566. >fetchAll();
  567. ?>
  568. <?php
  569. /*
  570. CREATE TABLE Guests (
  571. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  572. firstname VARCHAR(30) NOT NULL,
  573. lastname VARCHAR(30) NOT NULL,
  574. email VARCHAR(50),
  575. reg_date TIMESTAMP
  576. )
  577. */
  578. $servername = "localhost";
  579. $username = "username";
  580. $password = "password";
  581. $
  582. dbname = "test";
  583. try {
  584. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
  585. $password);
  586. // set the PDO error mode to exception
  587. $conn
  588. -
  589. >setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  590. // prepare sql and bind paramete
  591. rs
  592. $stmt = $conn
  593. -
  594. >prepare("INSERT INTO Guests (firstname, lastname, email)
  595. VALUES (:firstname, :lastname, :email)");
  596. $stmt
  597. -
  598. >bindParam(':firstname', $firstname);
  599. $stmt
  600. -
  601. >bindParam(':lastname', $lastname);
  602. $stmt
  603. -
  604. >bindParam(':email', $emai
  605. l);
  606. // insert a row
  607. $firstname = "John";
  608. $lastname = "Doe";
  609. $email = "john@example.com";
  610. $stmt
  611. -
  612. >execute();
  613. // insert another row
  614. $firstname = "Mary";
  615. $lastname = "Moe";
  616. $email = "mary@example.com";
  617. $stmt
  618. -
  619. >execute();
  620. // insert another row
  621. $firstname = "Julie";
  622. $lastname = "Dooley";
  623. $email = "julie@example.com";
  624. $stmt
  625. -
  626. >execute();
  627. echo "New records created successfully";
  628. }
  629. catch(PDOException $e)
  630. {
  631. echo "Error: "
  632. . $e
  633. -
  634. >getMessage();
  635. }
  636. $conn = null;
  637. ?>
  638. <?php
  639. $servername = "localhost";
  640. $username = "username";
  641. $password = "password";
  642. $dbname = "test";
  643. try {
  644. $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,
  645. $password);
  646. $conn
  647. -
  648. >setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  649. $stmt = $conn
  650. -
  651. >prepare("SELECT id, firstname, lastname FROM Guests");
  652. $stmt
  653. -
  654. >execute();
  655. // set the resulting array to associative
  656. $result = $stmt
  657. -
  658. >setFetchMode(PDO::FETCH
  659. _ASSOC);
  660. while($row = $stmt
  661. -
  662. >fetch()) {
  663. echo $row['id'] . "
  664. \
  665. t";
  666. echo $row['firstname'] . "
  667. \
  668. t";
  669. echo $row['lastname'] . "
  670. \
  671. t";
  672. echo "<br />";
  673. }
  674. }
  675. catch(PDOException $e) {
  676. echo "Error: " . $e
  677. -
  678. >getMessage();
  679. }
  680. $conn = null;
  681. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement