NYCPHP Meetup

NYPHP.org

[nycphp-talk] oci8 bind error

Daniel Convissor danielc at analysisandsolutions.com
Wed Jul 1 17:51:56 EDT 2009


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



More information about the talk mailing list