Last active
February 26, 2017 00:21
-
-
Save phpdave/b9c36e74798522b707f4ad7f2820bef4 to your computer and use it in GitHub Desktop.
Testing out the latest version of PHP7 and DB2 adapter on spaces.litmis.com. Also trying out new database features in IBM i 7.3. http://spaces.litmis.com:61184/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$db2Connection = doConnect(); | |
dosql("CREATE OR REPLACE TABLE DJTGR_D.ATEST1 (ID BIGINT)"); | |
dosql("CREATE OR REPLACE TABLE DJTGR_D.ATEST2 (ID BIGINT)"); | |
dosql("SELECT * FROM QSYS2.TABLES WHERE TABLE_SCHEMA = 'DJTGR_D'"); | |
dosql("CALL QSYS2.GENERATE_SQL('MYTABLE4#', 'DJTGR_D', 'TABLE')"); | |
dosql("CALL QSYS2.GENERATE_SQL('MY%', 'DJTGR_D', 'TABLE')"); | |
dosql("select current date from sysibm.sysdummy1"); | |
dosql("CREATE OR REPLACE TABLE DJTGR_D.MYTABLE4# ( | |
ID BIGINT GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1, NO ORDER, NO CYCLE, NO MINVALUE, NO MAXVALUE, CACHE 20) NOT HIDDEN , | |
FIRSTNAME# CHARACTER (30) NOT HIDDEN , | |
#LASTNAME CHARACTER (50) NOT HIDDEN , | |
PRIMARY KEY (ID) ) NOT VOLATILE "); | |
//dosql("CALL QSYS2.QCMDEXC('ADDRPYLE SEQNBR(1500) MSGID(CPA32B2) RPY(''I'')')"); | |
//dosql("CALL QSYS2.QCMDEXC('CHGJOB INQMSGRPY(*SYSRPYL)')"); | |
dosql("INSERT INTO DJTGR_D.MYTABLE3# (FIRSTNAME#,#LASTNAME) VALUES ('BOB','SMITH')"); | |
dosql("SELECT * FROM DJTGR_D.MYTABLE3#"); | |
dosql("SELECT CURRENT SERVER CONCAT ' is running ' CONCAT PTF_GROUP_TARGET_RELEASE CONCAT ' with TR level: ' CONCAT PTF_GROUP_LEVEL AS TR_LEVEL FROM QSYS2.GROUP_PTF_INFO WHERE PTF_GROUP_DESCRIPTION = 'TE | |
dosql("CREATE OR REPLACE PROCEDURE DJTGR_D.FETCHBYID (IN PARM_ID BIGINT) | |
LANGUAGE SQL | |
DYNAMIC RESULT SETS 1 | |
BEGIN | |
DECLARE c1 CURSOR FOR | |
SELECT * | |
FROM DJTGR_D.MYTABLE3# | |
WHERE ID = PARM_ID; | |
OPEN c1; | |
END;"); | |
dosql("Call DJTGR_D.FETCHBYID(?)",array(array("name"=>"ID","value"=>1))); | |
/** | |
* dosql ::HACK:: using global db2connection to call the sql passed | |
*/ | |
function dosql($sql_query,$parms=array()) | |
{ | |
echo "<h2>Did some SQL</h2>"; | |
echo "Ran: $sql_query with parms "; | |
Global $db2Connection; | |
if (strpos($sql_query, 'GENERATE_SQL') !== false) { | |
echo '<br>Running db2_exec<br>'; | |
$sql_statement = db2_exec($db2Connection,$sql_query); | |
} | |
else | |
{ | |
$sql_statement = db2_prepare($db2Connection, $sql_query); | |
if($sql_statement===false) | |
{ | |
echo "<span style=\"color:red;font-size:18px\">".db2_stmt_errormsg()."</span>"; | |
} | |
for($i=0;$i<count($parms);$i++) | |
{ | |
${$parms[$i]["name"]} = $parms[$i]["value"]; | |
db2_bind_param($sql_statement, $i+1, $parms[$i]["name"], DB2_PARAM_IN); | |
} | |
$result = db2_execute($sql_statement); | |
} | |
if($result===false) | |
{ | |
echo "<span style=\"color:red;font-size:18px\">".db2_stmt_errormsg()."</span>"; | |
} | |
$parmsExported = var_export($parms,true); | |
echo $parmsExported."<br>"; | |
$resultExported = var_export($result,true); | |
$rowExported = ""; | |
//print "First RS:";var_dump($sql_statement); | |
//$sql_statement2 = db2_next_result($sql_statement); | |
var_dump(db2_stmt_errormsg(),db2_conn_errormsg (),db2_stmt_errormsg($sql_statement2),db2_stmt_errormsg($sql_statement)); | |
//print "Second RS:";var_dump($sql_statement2); | |
print "Fetching first result set<br>"; | |
//$sql_statement2 = db2_next_result($sql_statement); | |
while($row = db2_fetch_assoc($sql_statement)) | |
{ | |
$rowExported .= var_export($row,true); | |
} | |
//$sql_statement = db2_exec($db2Connection,$sql_query); | |
print "Fetching more result set\n"; | |
while($nextResult = db2_next_result($sql_statement)) | |
{ | |
echo "<br>Found Another result set<br>"; | |
while($row = db2_fetch_assoc($nextResult)) | |
{ | |
$rowExported .= var_export($row,true); | |
} | |
} | |
if($nextResult == false) | |
{ | |
echo '<br>End fetching more result sets<br>'; | |
} | |
$statementError=db2_stmt_errormsg($sql_statement) ; | |
echo "Result:".$resultExported."<br>"; | |
if($statementError!="") | |
echo "Statement Error:".db2_stmt_errormsg($sql_statement)."<br>"; | |
echo "Fetched:".$rowExported."<br>"; | |
} | |
function doconnect() | |
{ | |
$database = '*LOCAL'; | |
$username = 'USRYB87R'; | |
$password = 'removed'; | |
$db2Connection = db2_connect($database , $username , $password ); | |
echo 'Connection Result for '.$database.':'; | |
var_dump($db2Connection); | |
echo '<br>'; | |
if(!$db2Connection) | |
{ | |
echo 'db2_conn_error():'.db2_conn_error()."<br>"; | |
echo 'db2_conn_errormsg():'.db2_conn_errormsg()."<br>"; | |
} | |
return $db2Connection; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment