使用db2更新数据库

时间:2016-10-10 09:33:33

标签: php db2

我需要帮助将这些mysql行转换为Db2,我不确定要使用它。

我需要转换的行是mysql_select_db('P510F');在那里添加p510f是对的吗?或者是否有其他东西存在?      

        $conn = db2_connect($dbhost, $dbuser, $dbpass);

        if(! $conn ) {
           die('Could not connect: ');
        }

        $emp_id = $_POST['emp_id'];
        $emp_salary = $_POST['emp_salary'];

        $sql = "UPDATE p510F ". "SET P510INF = $emp_salary ". 
           "WHERE P510KEY = $emp_id" ;
        mysql_select_db('P510F');
        $retval =  db2_exec( $sql, $conn );

        if(! $retval ) {
           die('Could not update data: ' . mysql_error());
        }
        echo "Updated data successfully\n";

        db2_close($conn);
     }else {
        ?>

断裂

           <form method = "post" action = "<?php $_PHP_SELF ?>">
              <table width = "400" border =" 0" cellspacing = "1" 
                 cellpadding = "2">

                 <tr>
                    <td width = "100">Employee ID</td>
                    <td><input name = "emp_id" type = "text" 
                       id = "emp_id"></td>
                 </tr>

                 <tr>
                    <td width = "100">Employee Salary</td>
                    <td><input name = "emp_salary" type = "text" 
                       id = "emp_salary"></td>
                 </tr>

                 <tr>
                    <td width = "100"> </td>
                    <td> </td>
                 </tr>

                 <tr>
                    <td width = "100"> </td>
                    <td>
                       <input name = "update" type = "submit" 
                          id = "update" value = "Update">
                    </td>
                 </tr>

              </table>
           </form>
        <?php
     }
  ?>

1 个答案:

答案 0 :(得分:0)

你能尝试这样的事吗?

$database = 'P510F';
$user = 'db2inst1';
$password = 'password';
$hostname = 'localhost';
$port = 50000;


$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;" .
  "HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');


        if(! $conn ) {
           die('Could not connect: '. db2_conn_error());
        }

        $emp_id = $_POST['emp_id'];
        $emp_salary = $_POST['emp_salary'];

        $sql = "UPDATE p510KEY ". "SET P510INF = $emp_salary ". 
           "WHERE P510KEY = $emp_id" ;

        $stmt = db2_prepare($conn, $sql);

 if ($stmt) {
      $result = db2_execute($stmt);
      if (!$result) {
         echo "exec errormsg: " .db2_stmt_errormsg($stmt);
      }
              echo "Updated data successfully\n";

   } else {
         echo "exec errormsg: " . db2_stmt_errormsg($stmt);
   }
   db2_close($conn);

     }else {
        ?>