/ Published in: PHP
Connect to and query a db2 database using php.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
$db_connection = database_connect(); $sql = "SELECT..."; //if using db2_bind_param.... //$sql = "SELECT * FROM table WHERE id = ?"; //$id = '123'; $stmt = db2_prepare($db_connection, $sql); //db2_bind_param($stmt,1,"id",DB2_PARAM_IN); $error = db2_stmt_errormsg(); if(db2_execute($stmt)){ while ($row = db2_fetch_object($stmt)){ $x = $row->x; $results[] = $row; } }else{ } function database_connect() { $db_name = ''; $usr_name = ''; $password = ''; $conn_resource = db2_connect($db_name, $usr_name, $password); if ($conn_resource) { //dd() is a drupal function. replace w/ echo() dd('Connection to database succeeded.'); return $conn_resource; } else { dd('Connection to database failed.'); dd('SQLSTATE: ' . db2_conn_error()); dd('Message: ' . db2_conn_errormsg()); } }