db2 connect and query


/ Published in: PHP
Save to your folder(s)

Connect to and query a db2 database using php.


Copy this code and paste it in your HTML
  1. $db_connection = database_connect();
  2.  
  3. $sql = "SELECT...";
  4.  
  5. //if using db2_bind_param....
  6. //$sql = "SELECT * FROM table WHERE id = ?";
  7. //$id = '123';
  8.  
  9. $stmt = db2_prepare($db_connection, $sql);
  10.  
  11. //db2_bind_param($stmt,1,"id",DB2_PARAM_IN);
  12.  
  13. $error = db2_stmt_errormsg();
  14.  
  15. if(db2_execute($stmt)){
  16.  
  17. while ($row = db2_fetch_object($stmt)){
  18.  
  19. $x = $row->x;
  20. $results[] = $row;
  21.  
  22. }
  23.  
  24. }else{
  25.  
  26.  
  27.  
  28. }
  29.  
  30. function database_connect() {
  31.  
  32. $db_name = '';
  33. $usr_name = '';
  34. $password = '';
  35.  
  36. $conn_resource = db2_connect($db_name, $usr_name, $password);
  37.  
  38. if ($conn_resource) {
  39.  
  40. //dd() is a drupal function. replace w/ echo()
  41. dd('Connection to database succeeded.');
  42. return $conn_resource;
  43.  
  44. } else {
  45.  
  46. dd('Connection to database failed.');
  47. dd('SQLSTATE: ' . db2_conn_error());
  48. dd('Message: ' . db2_conn_errormsg());
  49.  
  50. }
  51.  
  52. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.