如何通过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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐