dta43039 2019-01-15 19:59
浏览 108

如何通过PHP代码修复到SQL数据库的连接

I am working in Eclipse on a php project where I connect to an sql database and through php use insert statments to put data into a certain databank. When I first ran the code I automatically received this error..

Fatal error: Uncaught Error: Call to undefined function oci_connect() in C:\Users\Karl\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\htdocs\M4_zweiter_teil\M4_teil2_phpcode.php:7 Stack trace: #0 {main}thrown in C:\Users\Karl\eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\htdocs\M4_zweiter_teil\M4_teil2_phpcode.php on line 7

My code is the following

```<?php
$user = 'a01547603';
  $pass = 'dbs18';
  $database = 'jdbc:oracle:thin:@131.130.122.4:1521:lab';

  // establish database connection
  $conn = oci_connect($user, $pass, $database);
  if (!$conn) exit;
?>

<html>
<head>
</head>
<body>
  <div>
    <form id='searchform' action='index.php' method='get'>
      <a href='index.php'>Alle von betreuen</a> ---
      Suche nach Mitarbeiternummer: 
      <input id='search' name='search' type='text' size='20' value='<?php 
echo $_GET['search']; ?>' />
      <input id='submit' type='submit' value='Los!' />
    </form>
  </div>
<?php
  // check if search view of list view
  if (isset($_GET['search'])) {
    $sql = "SELECT * FROM betreuen  WHERE MNr like '%" . $_GET['search'] . 
"%'";
  } else {
    $sql = "SELECT * FROM betreuen";
  }

  // execute sql statement
  $stmt = oci_parse($conn, $sql);
  oci_execute($stmt);
?>
  <table style='border: 1px solid #DDDDDD'>
    <thead>
      <tr>
        <th>MNr</th>
        <th>PINr</th>
        <th>KID</th>
      </tr>
    </thead>
    <tbody>
<?php
  // fetch rows of the executed sql query
  while ($row = oci_fetch_assoc($stmt)) {
    echo "<tr>";
    echo "<td>" . $row['MINr'] . "</td>";
    echo "<td>" . $row['PINr'] . "</td>";
    echo "<td>" . $row['KID'] . "</td>";
    echo "</tr>";
  }
?>
    </tbody>
  </table>
<div>Insgesamt <?php echo oci_num_rows($stmt); ?> Betreuer gefunden!</div>
<?php  oci_free_statement($stmt); ?>

<div>
  <form id='insertform' action='index.php' method='get'>
     Neue Person einfuegen:
    <table style='border: 1px solid #DDDDDD'>
      <thead>
        <tr>
          <th>MNr</th>
          <th>PINr</th>
          <th>KID</th>
        </tr>
      </thead>
      <tbody>
         <tr>
            <td>
               <input id='MNr' name='MNr' type='text' size='10' 
value='<?php echo $_GET['MNr']; ?>' />
                </td>
                <td>
                   <input id='PINr' name='PINr' type='text' size='20' 
value='<?php echo $_GET['PINr']; ?>' />
                </td>
            <td>
            <input id='KID' name='KID' type='text' size='20' 
value='<?php echo $_GET['KID']; ?>' />
            </td>
          </tr>
           </tbody>
        </table>
        <input id='submit' type='submit' value='Insert!' />
  </form>
 </div>


<?php
  //Handle insert
  if (isset($_GET['MNr'])) 
  {
    //Prepare insert statementd
    $sql = "INSERT INTO betreuen VALUES(" . $_GET['MNr'] . ",'"  . 
$_GET['PINr'] . "','" . $_GET['KID'] . "')";
    //Parse and execute statement
    $insert = oci_parse($conn, $sql);
    oci_execute($insert);
    $conn_err=oci_error($conn);
    $insert_err=oci_error($insert);
    if(!$conn_err & !$insert_err){
    print("Successfully inserted");
    print("<br>");
    }
    //Print potential errors and warnings
    else{
        print($conn_err);
       print_r($insert_err);
       print("<br>");
    }
    oci_free_statement($insert);
  } 
?>
</body>
</html>
   ```

Of course there is some kind of problem with the connection so I looked for answers to this problem and I found on stackoverflow this answer

Whenever you connecting Oracle Database , try to use 32 Bit oracle client libraries, Since XAMP PHP is compiled with 32 Bit(Though you have 64 Bit windows Machine) Download Oracle Client from Download From here Paste it in C:\instantclient_12_1 Then Set the path to above in System Environment Variable Then Go to C:\xampp\php\php.ini and uncomment extension=php_oci8_12c.dll Then Restart the XAMP and it should work without any Issue.

I tried following through these steps but the problem is, they are to vague for me to understand. I am new to eclipse, php and Java. This is what I have done so far for each step.

1) Download Oracle Client from Download From here and Paste it in C:\instantclient_12_1.

That I did with out any problem.

2)Then Set the path to above in System Environment Variable.

This I didnt understand whether you do this in eclipse or on the computer itself. I tried both but could not figure it out in eclipse and while searching it up online I saw a video where the man did something on his PC in system environment variable. I tried this on my computer and eclipse failed to open and would not open (I later fixed this problem).

3)Then Go to C:\xampp\php\php.ini and uncomment extension=php_oci8_12c.dll

I dont know what xampp is but I found my php.ini file. I did not find extension=php_oci8_12c.dll rather just extension=php_oci8_12c with out the .dll So I uncommented that.

5)Then Restart the XAMP and it should work without any Issue. I dont have this and I dont know if I need this?

Of course this didnt work and I know I am doing something wrong but I don't Know what.

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误
    • ¥15 用visual studi code完成html页面
    • ¥15 聚类分析或者python进行数据分析
    • ¥15 三菱伺服电机按启动按钮有使能但不动作
    • ¥15 js,页面2返回页面1时定位进入的设备
    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复