PHP script to update an IBM i physical file

The place for general PHP questions and hints for PHP on IBM i
Post Reply
patr9257
Posts: 7
Joined: Wed Jun 24, 2015 4:31 pm

PHP script to update an IBM i physical file

Post by patr9257 » Thu Sep 17, 2015 9:37 pm

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

scottgcampbell
Posts: 187
Joined: Wed Apr 22, 2009 2:29 pm
Location: Edmonton, AB, Canada

Re: PHP script to update an IBM i physical file

Post by scottgcampbell » Mon Sep 21, 2015 2:09 pm

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

patr9257
Posts: 7
Joined: Wed Jun 24, 2015 4:31 pm

Re: PHP script to update an IBM i physical file

Post by patr9257 » Mon Sep 21, 2015 2:19 pm

Scott,

Yes, that is a big help. Thanks.

judy6640
Posts: 8
Joined: Sat Jun 17, 2017 10:07 am

Re: PHP script to update an IBM i physical file

Post by judy6640 » Sat Jun 17, 2017 11:34 am

How much PHP language is effective for solving various problems?

Post Reply