Page 1 of 1

PHP script to update an IBM i physical file

Posted: Thu Sep 17, 2015 9:37 pm
by patr9257
I am writing a PHP script to update a single record in a PF on our IBM i (AS/400). I will be updating several (but not all) columns in the record. What is the safest and most efficient approach? Should I concatenate a string together with the variable names to create an SQL update query and use db2_prepare? Should I use an array to provide the data for the SQL update query? Can I use db2_bind_param with an update query? Can someone suggest an approach and help me with the syntax? Thanks, Pat

Re: PHP script to update an IBM i physical file

Posted: Mon Sep 21, 2015 2:09 pm
by scottgcampbell
The safest would be to use db2_prepare with parameter markers. So create your SQL update statement (dynamically if you are not always updating all of the fields)

Code: Select all

$sql = "UPDATE SOMELIB.SOMEFILE SET COL1=?,COL2=?,COL3=? WHERE KEY = ?" 
Build your array of parameters:

Code: Select all

$parms = array($col1,$col2,$col3,$key);
Prepare and execute the query with db2_prepare and db2_execute:

Code: Select all

// You would also want some error checking in here
$query_res = db2_prepare ( $conn, $sql);
$qry_err = db2_execute ( $query_res, $parms );
// Check how many rows were affected
$rows = db2_num_rows ( $query_res );
I have the prepare and execute in a class that I can reuse so if I want/need to make any changes to the database access it can be done in a single spot.

Hope that helps.
Scott

Re: PHP script to update an IBM i physical file

Posted: Mon Sep 21, 2015 2:19 pm
by patr9257
Scott,

Yes, that is a big help. Thanks.

Re: PHP script to update an IBM i physical file

Posted: Sat Jun 17, 2017 11:34 am
by judy6640
How much PHP language is effective for solving various problems?