NYCPHP Meetup

NYPHP.org

[nycphp-talk] oci8 bind error

Eric Gewirtz eric.gewirtz at solutiononecdi.com
Thu Jul 2 13:32:22 EDT 2009


Hi Daniel,

Thanks I truly appreciate your help - With some minor tweaks this code does
work so at least I know it is not our version of php on the SUN box. I'm not
yet sure why the bind works here and not in my sample because I also passed
in variables to the bind and I'm in the process of trying to get mine to
work.

I have now gone as far as dropping my table that I created outside the code
and I am creating the table inside the code and that has not helped either
(this should not matter and I'm glad that it didn't).

I can not wait to find the bug, I know it is something really dumb ...

If anyone else out there is trying to run Daniel's code, you need to

1. fix the Insert syntax, it is missing a parenthesis before the table field
is defined and then you would need closing parenthesis at the end.
2. $variable in the bind should be $field_value otherwise a null is inserted
into the table
3. even if bind fails the "insert worked" message will echo out because
there is no break or die to end the if else checking so it drops down to the
echo stmt that says the insert worked

SolutionOne
Eric Gewirtz
162 Turk Hill Road
Brewster NY, 10509
Phone: 845-729-7800
FAX:    845-279-5502


>-----Original Message-----
>From: talk-bounces at lists.nyphp.org [mailto:talk-bounces at lists.nyphp.org] On
>Behalf Of Daniel Convissor
>Sent: Wednesday, July 01, 2009 5:52 PM
>To: NYPHP Talk
>Subject: Re: [nycphp-talk] oci8 bind error
>
>Hey Eric:
>
>> To your point that this "oci_bind_by_name ($stmt, ":TESTNAME", "ERIC");"
>> should work - I do not get the oracle error when run this statment, I get
>a
>> PHP fatal error: Only variables can be passed by reference. This makes no
>> sense to me either.
>
>Binding allows input to and output from the database.  In the event there
>is output from the query/procedure on that binding, PHP needs a place to
>store said output.  That place is a variable.  Pass something other than
>a variable and PHP kvetches.
>
>I recently needed more space on my hard disk, so removed Oracle, thus I
>can't test your code myself.  Alas.  Anyway, try this out:
>
><?php
>
>/*
> * SET THESE.
> */
>
>$put_colon_on_placeholder = true;  // TRY THIS BOTH TRUE AND FALSE.
>
>$username = '';
>$password = '';
>$dbname = '';
>
>
>/*
> * Leave these alone (for the most part).
> */
>
>$table_name = 'dan_test';
>$field_name = 'the_field';
>$max_length = '25';
>$field_value = 'testing 123';
>
>
>/*
> * Connect.
> */
>$dbh = oci_connect($username, $password, $dbname);
>if (!$dbh) {
>    die('CONNECT ERROR: ' . oci_error() . "\n");
>}
>
>
>/*
> * Create table.
> */
>
>$statement = 'CREATE TABLE ' . $table_name . ' '
>    . $field_name . ' VARCHAR2(' . $max_length . ')';
>echo $statement . "\n";
>
>$sth = oci_parse($dbh, $statement);
>if ($sth) {
>    if (!oci_execute($sth, OCI_DEFAULT)) {
>        die('EXECUTE ERROR: ' . oci_error($sth) . "\n");
>    }
>
>    if (!oci_free_statement($sth)) {
>        echo 'FREE ERROR: ' . oci_error($dbh) . "\n";
>    }
>} else {
>    die('STATEMENT PARSE ERROR: ' . oci_error($dbh) . "\n");
>}
>
>
>/*
> * Let's try binding.
> */
>
>if ($put_colon_on_placeholder) {
>    $ph_name = ':' . $field_name;
>} else {
>    $ph_name = $field_name;
>}
>$statement = 'INSERT INTO ' . $table_name
>    . ' (' . $field_name
>    . ') VALUES (' . $ph_name . ')';
>echo $statement . "\n";
>
>$sth = oci_parse($dbh, $statement);
>if ($sth) {
>    $bind = oci_bind_by_name($sth, $ph_name, $variable,
>            $max_length, SQLT_CHR);
>    if ($bind) {
>        if (!oci_execute($sth, OCI_COMMIT_ON_SUCCESS)) {
>            echo 'EXECUTE ERROR: ' . oci_error($sth) . "\n";
>        }
>    } else {
>        echo 'BIND ERROR: ' . oci_error($sth) . "\n";
>    }
>
>    if (!oci_free_statement($sth)) {
>        echo 'FREE ERROR: ' . oci_error($dbh) . "\n";
>    }
>
>    echo 'The insert seems to have worked.' . "\n";
>} else {
>    echo 'STATEMENT PARSE ERROR: ' . oci_error($dbh) . "\n";
>}
>
>
>/*
> * Drop table.
> */
>
>$statement = 'DROP TABLE ' . $table_name;
>echo $statement . "\n";
>
>$sth = oci_parse($dbh, $statement);
>if ($sth) {
>    if (!oci_execute($sth, OCI_DEFAULT)) {
>        die('EXECUTE ERROR: ' . oci_error($sth) . "\n");
>    }
>
>    if (!oci_free_statement($sth)) {
>        echo 'FREE ERROR: ' . oci_error($dbh) . "\n";
>    }
>} else {
>    die('STATEMENT PARSE ERROR: ' . oci_error($dbh) . "\n");
>}
>
>
>if (!oci_close($dbh)) {
>    echo 'CLOSE ERROR: ' . oci_error($dbh) . "\n";
>}
>
>--
> T H E   A N A L Y S I S   A N D   S O L U T I O N S   C O M P A N Y
>            data intensive web and database programming
>                http://www.AnalysisAndSolutions.com/
> 4015 7th Ave #4, Brooklyn NY 11232  v: 718-854-0335 f: 718-854-0409
>_______________________________________________
>New York PHP User Group Community Talk Mailing List
>http://lists.nyphp.org/mailman/listinfo/talk
>
>http://www.nyphp.org/show_participation.php





More information about the talk mailing list