/    Sign up×
Community /Pin to ProfileBookmark

I am trying to do an Insert from php to db2. Here is my sql:

[code]
include “db2conn.php”;
$sqldb2=”INSERT into inet.incoi_contact_info (id, first_nme, last_nme, phone_nbr, department,
lst_mnt_opr_id, lst_maint_tsmp)
values (:id, :first_nme, :last_nme, :phone_nbr, :department, :session_userid, current_date) “;

$sql_argdb2= “id” . DELIM . $id . DELIM .
“first_nme” . DELIM . $first_nme . DELIM .
“last_nme” . DELIM . $last_nme . DELIM .
“phone_nbr” . DELIM . $phone_nbr . DELIM .
“department” . DELIM . $department . DELIM .
“session_userid” . DELIM . $session_userid;
$DB2->my_sqlbnd($sqldb2, $sql_argdb2);
[/code]

Here is my db2conn.php file:

[code]
if (!defined($inc_db2conn))
{
$inc_db2conn=1;
class mydb2
{ //CLASS DEFN. for mydb to make selects easy…and quiet
// USAGE: (1)
// include (“db2conn.php”);
// $conn = new mydb;
// $conn->my_conn();
// $sel_stmt = “Select something from somewhere…”;
// $conn->my_stmt($sel_stmt);
// for ($i=0;$i < $conn->numrows;$i++)
// {
// print $conn->results[“SOMECOLUMNNAME1”][$i];
// print “, “;
// print $conn->results[“SOMECOLUMNNAME2”][$i];
// }
// $conn->my_close();
// (2)
// include (“db2conn.php”);
// $conn = new mydb;
// $sel_stmt = “Select something from somewhere…”;
// $conn->my_sel($sel_stmt);
// for (…..refer to above)
//
// **note no my_close is needed b/c my_sel does it all.

var $c; // new connection to mysql db
var $sqlstmt; //database object created as result of query
var $querystr; //string containing query passed in
var $results; //Results array resulting from any query
var $res; //Results resource returned by executing odbc query
var $numrows; //Number of rows affected from query
var $row; //Row Object containg column values of query
var $i; //Current row pointer
var $dsn; //Value containing alias to db2 subsystem db2test, db2car, db2prod
var $user; //User id used to authenticate to db2
var $password; //Password for said user
var $ORA_ERR; //odbc error message number
var $ORA_ERR_DESC; //odbc error mesage

#function mydb2($db, $uid, $pwd)
#{
# global $my_db_userid, $my_db_passwd, $my_db_instance;
# this->dsn = $my_db_instance;
# $this->user = $my_db_userid;
# $this->password = $my_db_passwd;
#}

function _handle_connection_failure()
{
global $SCRIPT_FILENAME,
$SERVER_ADDR,
$SERVER_NAME,
$SCRIPT_NAME;

$date=date(“Y-m-d H:i:s”);
$offensive_statement=preg_replace(“‘[rns]+'”, ” “, $this->querystr);
$oracle_error=preg_replace(“‘[rns]+'”, ” “, odbc_errormsg());

print(“<pre>
Hi there. My name is ‘$SERVER_NAME’ at ‘$SERVER_ADDR’.
The date is $date.
I’m writing you today because I’ve found a Database error in the web page
‘$SCRIPT_NAME’, in the file ‘$file’, specifically on line ‘$line’.

As it turns out, the offensive statement was as follows…
‘$offensive_statement’

… and the error that the Database gave me was …
‘$oracle_error’.

Hey, though, you have a FANTASTIC day!!
</pre>”);
exit;
}

function my_err($stmt)
{
$this->ORA_ERR = odbc_error();
$this->ORA_ERR_DESC = “SQL: (” . $stmt . “) ERR: ” . odbc_errormsg();
}

function my_conn()
{ //Create database connection using bg-id
global $my_db2_userid, $my_db2_passwd, $my_db2_instance;
//echo “instance: ” . $my_db2_instance . “<br>userid: ” . $my_db2_userid . “<br>password: ” . $my_db2_passwd . “<br>”;
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$this->i = 0;
$this->c = odbc_connect(“$my_db2_instance”,”$my_db2_userid”,”$my_db2_passwd”) or
$this->_handle_connection_failure();
error_reporting(E_ERROR | E_WARNING | E_PARSE);
}

function my_upd($stmt)
{ //Execute passed in SQL and Return $results
$this->querystr = $stmt;
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$this->my_conn();
$this->res = odbc_prepare($this->c, $stmt) or die (odbc_errormsg());
$rc = odbc_execute($this->res) or die (odbc_errormsg());
$this->my_err($stmt);
$this->numrows = odbc_num_rows($this->res);
if ( eregi(‘insert’, $stmt))
{
if (( $this->numrows == 0 ) && ( $this->ORA_ERR == 1 ))
{
print “Insert failed! Try an UPDATE! n”;
}
}
$this->my_close();
}

function my_sel($stmt)
{ //Execute passed in SQL and Return $results
$this->querystr = $stmt;
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$this->my_conn();
print (“<br>SQL_EXEC: (” . $stmt . “)”);
$this->res = odbc_exec($this->c, $stmt);
$this->my_err($stmt);
if (!$this->res) $this->_handle_connection_failure();
$this->make_results($this->res);
$this->my_close();
error_reporting(E_ERROR | E_WARNING | E_PARSE);
}

function make_results($res)
{//Make an associative array of the results and Return $results
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$i = 0;
$j = 0;
unset($temp_fieldnames);

while(odbc_fetch_row($this->res))
{

//Build tempory
for ($j = 1; $j <= odbc_num_fields($this->res); $j++)
{
$field_name = odbc_field_name($this->res, $j);
//*debug* print (“<br>Making Results: Field name: (” . $field_name . “)”);
$temp_fieldnames[$j] = $field_name;
$this->results[$field_name][$i] = odbc_result($this->res, $field_name);
//*debug* print (“<br>Making Results: Field Value (” . $this->results[$field_name][$i] . “)”);
}

//$this->results[$i] = $ar;
$i++;
}
$this->numrows = $i;
}

function my_sqlbnd()
{ //Execute bind and sqlstatement, dynamic args. (SQL, VARNAME, VARVALUE, …)

error_reporting(E_ERROR | E_WARNING | E_PARSE);
$this->my_conn();
$stmt = func_get_arg (0);
#$numargs = func_num_args();
$strarg = func_get_arg(1);
$args = split(DELIM, $strarg);
$numargs = count($args);
$z = 0;
print “<br>Array count is: ” . $numargs;
if ((($numargs) % 2) <> 0)
{
print “<br>Sorry, invalid # arguments: ” . $numargs . “n”;
}
else
{
//Find all occurences of Host Variables (whole words starting with “:” and ending on a word boundry) in the sql stmt
preg_match_all(“/:[A-Za-z0-9_]+?b/”, $stmt, $bindarray);
//*debug* print_r ($bindarray[0]);
//Replace all occurences of host variables in string with ODBC compliant “?”‘s
$stmt = str_replace($bindarray[0], “?”, $stmt);
print (“<br>SQL: (” . $stmt . “)”);
//Complete foreach loop to create array that can be passed to odbc_execute as values for ?’s
$hostvar_val_arr = array();
foreach ($bindarray[0] as $hostvar) {
print(“<br>Looking for hostvar (” . $hostvar . “)”);
$arr_key = array_search(substr($hostvar, 1), $args);
print (“<br>array key: (” . $arr_key . “)”);
array_push($hostvar_val_arr, $args[$arr_key + 1]);
}
print (“<br><br>HOSTVAR: —>”);
print_r ($hostvar_val_arr);
//Prepare and execute the newly formed SQL and value array
$this->res = odbc_prepare($this->c, $stmt) or die (“PREPARE: odbc error: ” . odbc_errormsg());
$rc = odbc_execute($this->res, $hostvar_val_arr ) or die (“EXECUTE: odbc error: ” .odbc_errormsg());
$this->my_err($stmt);
$this->numrows = odbc_num_rows($this->res);
$this->my_close();
} // end if not enough ARGS
error_reporting(E_ERROR | E_WARNING | E_PARSE);
}

function my_stmt($stmt)
{ //Execute passed in SQL and Return $results
$this->querystr = $stmt;
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$this->my_conn();
//echo “calling odbc_exec($this->c, $stmt)”;
$this->res = @odbc_exec($this->c, $stmt) or die(odbc_errormsg());
$this->numrows = odbc_num_rows ($this->res);
$this->make_results($this->res);
$this->my_close();
}

function my_close()
{ //Close mysql connection
error_reporting(E_ERROR | E_WARNING | E_PARSE);
odbc_free_result($this->res);
odbc_Close($this->c);
}

}//end of class
} //end of ifdef statement
[/code]

Could someone look at the my_sqlbnd method and see why I keep getting this error:

Warning: odbc_execute(): SQL error: [unixODBC][IBM][CLI Driver] CLI0100E Wrong number of parameters. SQLSTATE=07001, SQL state 07001 in SQLExecute in /home/prgjr1/public_html/ra-apps/inc/db2conn.php on line 193
EXECUTE: odbc error: [unixODBC][IBM][CLI Driver] CLI0100E Wrong number of parameters. SQLSTATE=07001

thanks!!!!!!!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@bokehMay 22.2006 — Have you read the error message? It tells you everything!

[B]File: [/B][I]db2conn.php [/I]

[B]Line: [/B][I]193[/I]

[B]Function: [/B][I]odbc_execute()[/I]

[B]Problem: [/B][I]wrong number of parameters[/I]
Copy linkTweet thisAlerts:
@jrthor2authorMay 23.2006 — Of course I read the error, I wouldn't post here if I didn't already try to fix it. printing out the parameters shows the correct number of parameters, that's why I posted my question.
Copy linkTweet thisAlerts:
@bokehMay 23.2006 — Well starting at the top your first problem is here: [code=php]if (!defined($inc_db2conn))[/code]defined is for constants not variables so will only ever evalute to [I]true[/I]. From that point on your indenting is out making the code hard to read quickly but for some reason your class is part of a conditional statement.

By the way which line is 193? You haven't marked it.
Copy linkTweet thisAlerts:
@jrthor2authorMay 23.2006 — Line 193 is this line:

$rc = odbc_execute($this->res, $hostvar_val_arr ) or die ("EXECUTE: odbc error: " .odbc_errormsg());
Copy linkTweet thisAlerts:
@jrthor2authorMay 23.2006 — Ok, I commented out the line at the top as you mentioned. I tried again and I I am falling into this logic:

if ((($numargs) % 2) <> 0)

{

print "<br>Sorry, invalid # arguments: " . $numargs . "n";

}
×

Success!

Help @jrthor2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...