kijato

oracle_pdo-oci_test.php

Oct 5th, 2020
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <html>
  2. <head><style>*{font-family:Verdana}table,td,th{border-collapse:collapse;border:1px solid lightgray}</style></head>
  3. <body>
  4.  
  5.  
  6.  
  7. <h1>pdo</h1>
  8. <sup>http://www.learn-coding.today/php_pdo_oracle.php</sup><br>
  9. <?php
  10. #$param = $_POST;
  11. $db_username = "guest";
  12. $db_password = "guest";
  13. $db = "oci:dbname=XE";
  14. try {
  15.     $conn = new PDO($db,$db_username,$db_password);
  16.     #$name = $param['module'];
  17.     #$file = $param['file'];
  18.     #$stmt = $conn->exec("INSERT INTO AL_MODULE (AL_MODULENAME, AL_MODULEFILE) VALUES ('$name', '$file')");
  19.    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  20.    
  21.     $stmt = $conn->query('SELECT * FROM takaros_hrszek WHERE ROWNUM < 5');
  22.     while ($row = $stmt->fetch())
  23.     {
  24.         echo $row[0] .", ".  $row[1] . ", " . $row[2] .", " . $row[3] .", " . $row[4] ."<br>";
  25.     }
  26.  
  27. } catch(PDOException $e) {
  28.     echo 'ERROR: ' . $e->getMessage();
  29. }
  30. ?>
  31.  
  32.  
  33.  
  34. <h1>oci</h1>
  35. <?php
  36. // Create connection to Oracle
  37. $conn = oci_connect("guest", "guest", "localhost/XE");
  38. if (!$conn) {
  39.    $m = oci_error();
  40.    echo $m['message'], "\n";
  41.    exit;
  42. }
  43. else {
  44.    print "Connected to Oracle!";
  45. }
  46. // Close the Oracle connection
  47. oci_close($conn);
  48. ?>
  49.  
  50.  
  51.  
  52. <h1>oci+</h1>
  53. <?php
  54.  
  55. $conn = oci_connect('guest', 'guest', 'localhost/XE');
  56. if (!$conn) {
  57.     $e = oci_error();
  58.     trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
  59. }
  60.  
  61. $stid = oci_parse($conn, 'SELECT *  FROM takaros_hrszek WHERE ROWNUM < 5');
  62. oci_execute($stid);
  63.  
  64. $nrows = oci_fetch_all($stid, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
  65.  
  66. echo "<p>".var_dump($res)."</p>";
  67.  
  68. // var_dump output is:
  69. //    2 rows fetched
  70. //    array(2) {
  71. //      ["POSTAL_CODE"]=>
  72. //      array(2) {
  73. //        [0]=>
  74. //        string(6) "00989x"
  75. //        [1]=>
  76. //        string(6) "10934x"
  77. //      }
  78. //      ["CITY"]=>
  79. //      array(2) {
  80. //        [0]=>
  81. //        string(4) "Roma"
  82. //        [1]=>
  83. //        string(6) "Venice"
  84. //      }
  85. //    }
  86.  
  87. // Pretty-print the results
  88. echo "<table>\n";
  89.  
  90. echo "<tr>";
  91. for ($i = 1; $i <= oci_num_fields($stid); $i++) {
  92.     echo "<th>".oci_field_name($stid, $i)."</th>";
  93. }
  94. echo "</tr>\n";
  95.  
  96. foreach ($res as $col) {
  97.     echo "<tr>\n";
  98.     foreach ($col as $item) {
  99.         echo "    <td>".($item !== null ? htmlentities($item, ENT_QUOTES) : "")."</td>";
  100.     }
  101.     echo "\n</tr>\n";
  102. }
  103. echo "</table>\n";
  104. echo "$nrows rows fetched<br>\n";
  105.  
  106. oci_free_statement($stid);
  107. oci_close($conn);
  108.  
  109. ?>
  110.  
  111.  
  112. </body>
  113. </html>
Add Comment
Please, Sign In to add comment