PHP/Oracle - Pulling data into array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philip D Heady

    PHP/Oracle - Pulling data into array

    Ok, as some of you may know I'm an Oracle newbie w/ PHP. I'd rather use
    MySQL but at the office here we use Oracle and boy do I have alot to learn.
    I'm starting to hate it after using MySQL!!

    --------------------------------------------------------------------------
    1) Is there a similar statement using PHP/Oracle functions as below for
    MySQL?
    --------------------------------------------------------------------------

    while (list ($key, $val) = each ($HTTP_POST_VAR S)) {
    print $key . " = " . $val . "<br>";
    }

    --------------------------------------------------------------------------
    2) Should I be using something like this or is there easier way to pull data
    into array?
    --------------------------------------------------------------------------

    // Start new Oracle cursor and query

    $q = "select * from inventory";

    $ora_cur = ora_do( $ora_conn, $q ) or die("Couldn't setup Oracle
    Cursor");

    if ( $ora_cur ) {

    // Figure out how many columns

    $numCols = ora_numcols( $ora_cur );

    // Get the first fetched row and put it in to our array...

    $row = array();

    // Loop through columns

    for( $i = 0; $i < $numCols; $i++ ){

    array_push( $row, ora_getcolumn( $ora_cur, $i ) );
    }
    array_push( $results, $row );

    // Fetch rows, one at a time, putting them in their own
    // array. Each row should be appended to the array of
    // results..

    // Get each row

    while ( ora_fetch( $ora_cur ) ){
    $row = array();

    // Loop through columns

    for( $i = 0; $i < $numCols; $i++ ){
    array_push( $row, ora_getcolumn( $ora_cur, $i ) );
    }
    array_push( $results, $row );
    }
    }


    while (list ($results, $row) = each ($HTTP_GET_VARS )) {
    print $results. " = " . $row . "<br>";
    }

    ?>

    --------------------------------------------------------------------------
    3) For some reason my html below does not get displayed on the page. After
    I submit the page is blank and just says database connected succesfully.
    Why is it stopping here??
    --------------------------------------------------------------------------

    <html><head><ti tle>etc...<body >etc...



  • gmuldoon

    #2
    Re: PHP/Oracle - Pulling data into array

    pdheady@comcast .net says...[color=blue]
    > Ok, as some of you may know I'm an Oracle newbie w/ PHP. I'd rather use
    > MySQL but at the office here we use Oracle and boy do I have alot to learn.
    > I'm starting to hate it after using MySQL!![/color]

    Philip,

    Suggest that you get ADODB http://php.weblogs.com/ADOdb/

    This is a database abstraction layer package, which may be a softer
    migration path for you than going from MySQL hard into the native OCI8
    library.

    If you do go that way, be sure to read up on how to query the DB using
    bind variables (supported in ADODB for Oracle, MS-SQL) and make sure you
    use them.

    Geoff M

    Comment

    • Philip D Heady

      #3
      Re: PHP/Oracle - Pulling data into array

      That link you provided does not work!

      I can post to oracle no problem, but pulling data is a pain for me.

      Here's an example that isn't working very well for me:

      $stmt = OCIParse($conn, "select * from inventory");

      OCIExecute($stm t);

      OCIFetchInto ($stmt, $row, OCI_ASSOC);

      while (list($key, $val) = each($row)) {

      print $key . " = " . $val . "<br>";

      }


      "gmuldoon" <gmuldoon_nospa m@scu.edu.au> wrote in message
      news:MPG.1a9aa4 48ef56b24e98969 6@news.asgard.n et.au...[color=blue]
      > pdheady@comcast .net says...[color=green]
      > > Ok, as some of you may know I'm an Oracle newbie w/ PHP. I'd rather use
      > > MySQL but at the office here we use Oracle and boy do I have alot to[/color][/color]
      learn.[color=blue][color=green]
      > > I'm starting to hate it after using MySQL!![/color]
      >
      > Philip,
      >
      > Suggest that you get ADODB http://php.weblogs.com/ADOdb/
      >
      > This is a database abstraction layer package, which may be a softer
      > migration path for you than going from MySQL hard into the native OCI8
      > library.
      >
      > If you do go that way, be sure to read up on how to query the DB using
      > bind variables (supported in ADODB for Oracle, MS-SQL) and make sure you
      > use them.
      >
      > Geoff M[/color]


      Comment

      • Andy Hassall

        #4
        Re: PHP/Oracle - Pulling data into array

        On Mon, 16 Feb 2004 16:32:32 -0500, "Philip D Heady" <pdheady@comcas t.net>
        wrote:
        [color=blue]
        >"gmuldoon" <gmuldoon_nospa m@scu.edu.au> wrote in message
        >news:MPG.1a9aa 448ef56b24e9896 96@news.asgard. net.au...[color=green]
        >> pdheady@comcast .net says...[color=darkred]
        >>> Ok, as some of you may know I'm an Oracle newbie w/ PHP. I'd rather use
        >>> MySQL but at the office here we use Oracle and boy do I have alot to[/color]
        >> learn.[color=darkred]
        >>> I'm starting to hate it after using MySQL!![/color]
        >>
        >> Philip,
        >>
        >> Suggest that you get ADODB http://php.weblogs.com/ADOdb/[/color]
        >
        >That link you provided does not work![/color]

        Works from here. Check your ISP.
        [color=blue]
        >I can post to oracle no problem, but pulling data is a pain for me.
        >
        >Here's an example that isn't working very well for me:
        >
        >$stmt = OCIParse($conn, "select * from inventory");
        >
        >OCIExecute($st mt);
        >
        >OCIFetchInto ($stmt, $row, OCI_ASSOC);
        >
        > while (list($key, $val) = each($row)) {
        >
        > print $key . " = " . $val . "<br>";
        >
        > }[/color]

        Other than the lack of error checking and assumption that it returns a single
        row, what about that isn't 'working very well' for you? Presumably you want to
        put the OCIFetchInto in a while loop. Like in the manual.

        <http://uk2.php.net/manual/en/function.ocifet chinto.php>

        But since you haven't said what's wrong, it's hard to suggest how to fix it.

        --
        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

        Comment

        • gmuldoon

          #5
          Re: PHP/Oracle - Pulling data into array

          pdheady@comcast .net says...[color=blue]
          > That link you provided does not work![/color]

          An example why not to top-post. Put you responses in-line.

          Capitalisation problem, try:

          [color=blue]
          > I can post to oracle no problem, but pulling data is a pain for me.
          >
          > Here's an example that isn't working very well for me:
          >
          > $stmt = OCIParse($conn, "select * from inventory");
          >
          > OCIExecute($stm t);
          >
          > OCIFetchInto ($stmt, $row, OCI_ASSOC);
          >
          > while (list($key, $val) = each($row)) {
          >
          > print $key . " = " . $val . "<br>";
          >
          > }
          >[/color]
          Some very rough sample snippets:

          OCI8 example of the way I code:

          $sql1="select item_id, item_desc from inventory";
          $stmt1=OCIParse ($conn, $sql1);
          OCIDefineByName ($stmt1, "item_id", &$id);
          OCIDefineByName ($stmt1, "item_desc" , &$desc);
          OCIExecute($stm t1);
          while(OCIFetch( $stmt1)){
          echo $id.' - '.$desc.'<br>';
          }
          OCILogoff($conn );

          ADODB example of the way I code:

          $type='some_typ e';
          $sql="select item_id, item_desc from inventory where item_type=:type ";
          $stmt=$conn->Prepare($sql );
          $conn->Parameter($stm t, $type, 'type'); // USE BIND VARIABLES
          $rs=$oraconn->Execute($stmt) ;
          if ($rs===false) die('Sorry, unable to retrieve items);
          while (!$rs->EOF ) {
          echo $rs->fields(item_id ).' - '$rs->fields(item_de sc).'<br>';
          $rs->moveNext();
          }

          Cheers,

          Geoff M

          Comment

          • Andy Hassall

            #6
            Re: PHP/Oracle - Pulling data into array

            On Mon, 16 Feb 2004 22:45:08 GMT, gmuldoon <gmuldoon_nospa m@scu.edu.au> wrote:
            [color=blue][color=green][color=darkred]
            >>>Suggest that you get ADODB http://php.weblogs.com/ADOdb/[/color]
            >>
            >>That link you provided does not work![/color]
            >
            >Capitalisati on problem, try:
            >http://php.weblogs.com/ADODB/[/color]

            They're running Windows so your original URL was (also) fine.

            <http://uptime.netcraft .com/up/graph/?host=php.weblo gs.com>

            --
            Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
            <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

            Comment

            • Philip D Heady

              #7
              Re: PHP/Oracle - Pulling data into array

              Thansk for the examples GMuldoon, and folks...!

              ---------------------------------------------------
              1) How would I query an item_id and return all the key/values to populate a
              form...
              ---------------------------------------------------

              "select * from inventory where item_id = '$item_id'";

              Then what?? Must I list every variable in OCIDefineByName ?? I would hope
              not...I should be able to pull key/val like I do in mySQL which right now
              looks much easier then oracle's pain in the ass way.

              $sql1="select * from inventory where item_id = '$item_id'";
              $stmt1=OCIParse ($conn, $sql1);
              OCIDefineByName ($stmt1, "item_id", &$id);
              OCIDefineByName ($stmt1, "item_desc" , &$desc);
              OCIExecute($stm t1);
              while(OCIFetch( $stmt1)){
              echo $id.' - '.$desc.'<br>';
              }
              OCILogoff($conn );

              ---------------------------------------------------
              2) What are the benefits to using ADODB??
              ---------------------------------------------------




              [color=blue]
              > That link you provided does not work![/color]

              An example why not to top-post. Put you responses in-line.

              Capitalisation problem, try:

              [color=blue]
              > I can post to oracle no problem, but pulling data is a pain for me.
              >
              > Here's an example that isn't working very well for me:
              >
              > $stmt = OCIParse($conn, "select * from inventory");
              >
              > OCIExecute($stm t);
              >
              > OCIFetchInto ($stmt, $row, OCI_ASSOC);
              >
              > while (list($key, $val) = each($row)) {
              >
              > print $key . " = " . $val . "<br>";
              >
              > }
              >[/color]
              Some very rough sample snippets:

              OCI8 example of the way I code:

              $sql1="select item_id, item_desc from inventory";
              $stmt1=OCIParse ($conn, $sql1);
              OCIDefineByName ($stmt1, "item_id", &$id);
              OCIDefineByName ($stmt1, "item_desc" , &$desc);
              OCIExecute($stm t1);
              while(OCIFetch( $stmt1)){
              echo $id.' - '.$desc.'<br>';
              }
              OCILogoff($conn );

              ADODB example of the way I code:

              $type='some_typ e';
              $sql="select item_id, item_desc from inventory where item_type=:type ";
              $stmt=$conn->Prepare($sql );
              $conn->Parameter($stm t, $type, 'type'); // USE BIND VARIABLES
              $rs=$oraconn->Execute($stmt) ;
              if ($rs===false) die('Sorry, unable to retrieve items);
              while (!$rs->EOF ) {
              echo $rs->fields(item_id ).' - '$rs->fields(item_de sc).'<br>';
              $rs->moveNext();
              }

              Cheers,

              Geoff M


              Comment

              • Andy Hassall

                #8
                Re: PHP/Oracle - Pulling data into array

                On Tue, 17 Feb 2004 17:52:31 -0500, "Philip D Heady" <pdheady@comcas t.net>
                wrote:
                [color=blue]
                >Thansk for the examples GMuldoon, and folks...!
                >
                >---------------------------------------------------
                >1) How would I query an item_id and return all the key/values to populate a
                >form...
                >---------------------------------------------------
                >
                >"select * from inventory where item_id = '$item_id'";[/color]

                No, completely wrong.
                [color=blue]
                >Then what?? Must I list every variable in OCIDefineByName ?? I would hope
                >not...I should be able to pull key/val like I do in mySQL which right now
                >looks much easier then oracle's pain in the ass way.[/color]

                OCIFetchInto is acceptable, and acts similarly to mysql_fetch_*. Have you read
                the documentation?

                OCIDefineByName more closely matches the way you're _supposed_ to use the OCI
                interface natively from C, and so is likely to be more efficient (untested).
                [color=blue]
                >$sql1="selec t * from inventory where item_id = '$item_id'";[/color]

                *bash over head - this is some thing you must never do*

                MySQL has the flaw that you have to stuff values into SQL statements, mixing
                DATA with SQL. This is Bad. Sensible databases use BIND VARIABLES,
                alternatively named PLACEHOLDERS.

                A properly rewritten version of above would look like:

                select * from inventory where item_id = :item_id

                Or:

                select * from inventory where item_id = ?

                Depending on what interface and database you're using.

                (Actually you'd probably also want to lose the '*' and replace it with a
                proper column list to insulate you from schema changes).

                Stuffing data into literal SQL causes several problems:

                (1) SQL injection attacks. You forget to escape quotes, and suddenly data
                becomes executed SQL. This is a severe security problem.

                (2) Oracle, and other databases, cache the execution plans of SQL. The first
                time it comes across a new statement, it works out the best way to execute it.
                Since it has a decent optimiser, this is a relatively expensive operation, as
                it does a fair bit of work to find out what's really the best way to do it from
                the many access paths it has available. But it caches this in memory, so when
                you execute it again later (you rarely only run something *once*) it doesn't
                have to go through the parsing and optimisation steps, it already knows HOW to
                execute it, it just has to plug in the values.

                But if you put literal values in an SQL statement, the text of the statement
                is different every time you execute it. So it can't use the cached plan, and
                has to re-parse it, and do all the optimisation again. This causes excessive
                "hard-parsing" and cripples your database.

                If you have bind variables/placeholders, then the execution plan is the same
                (since the SQL is the same) but you plug in the _data_ later.

                $sql = 'select * from inventory where item_id = :item_id';
                $stmt1 = OCIParse($conn, $sql1);

                if (!$stmt1) {
                // handle the error, with reference to OCIError()
                }

                OCIBindByName($ stmt, ':item_id', &$item_id, -1);
                [color=blue]
                >OCIDefineByNam e($stmt1, "item_id", &$id);
                >OCIDefineByNam e($stmt1, "item_desc" , &$desc);
                >OCIExecute($st mt1);
                >while(OCIFetch ($stmt1)){
                > echo $id.' - '.$desc.'<br>';
                >}[/color]

                You have a choice between using defines, or using OCIFetchInto which wraps
                this up for you and gives you PHP arrays. It's up to you. Read the
                documentation, make your own decision.
                [color=blue]
                >OCILogoff($con n);
                >
                >---------------------------------------------------
                >2) What are the benefits to using ADODB??
                >---------------------------------------------------[/color]

                ADODB has a manual. The very first chapter of the manual gives reasons why you
                might want to use ADODB.

                Geoff already gave you a reason why; it hides you from some of the OCI8
                interface, which is quite low-level, and gives you a higher-level uniform
                interface that works for many different types of database.
                [color=blue][color=green]
                >> That link you provided does not work![/color]
                >
                >An example why not to top-post. Put you responses in-line.[/color]

                You (Philip) didn't write this. But because you're still top-posting (look it
                up on Google) and further you're not quoting correctly, Geoff's reply is
                attributed to you in your reply.

                --
                Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

                Comment

                • Philip D Heady

                  #9
                  Re: PHP/Oracle - Pulling data into array

                  "You have a choice between using defines, or using OCIFetchInto which wraps
                  this up for you and gives you PHP arrays. It's up to you. Read the
                  documentation, make your own decision."

                  From your poor explanation, I would conclude that I three choices not two.
                  So which is it?

                  OCIDefineByName , OCIBindByName, or using OCIFetchInto

                  Perhaps you could be a little clearer.

                  Phil



                  "Andy Hassall" <andy@andyh.co. uk> wrote in message
                  news:q87530tuus 5so2m3ucbud47f6 2vgjqnhsg@4ax.c om...[color=blue]
                  > On Tue, 17 Feb 2004 17:52:31 -0500, "Philip D Heady" <pdheady@comcas t.net>
                  > wrote:
                  >[color=green]
                  > >Thansk for the examples GMuldoon, and folks...!
                  > >
                  > >---------------------------------------------------
                  > >1) How would I query an item_id and return all the key/values to populate[/color][/color]
                  a[color=blue][color=green]
                  > >form...
                  > >---------------------------------------------------
                  > >
                  > >"select * from inventory where item_id = '$item_id'";[/color]
                  >
                  > No, completely wrong.
                  >[color=green]
                  > >Then what?? Must I list every variable in OCIDefineByName ?? I would hope
                  > >not...I should be able to pull key/val like I do in mySQL which right now
                  > >looks much easier then oracle's pain in the ass way.[/color]
                  >
                  > OCIFetchInto is acceptable, and acts similarly to mysql_fetch_*. Have you[/color]
                  read[color=blue]
                  > the documentation?
                  >
                  > OCIDefineByName more closely matches the way you're _supposed_ to use the[/color]
                  OCI[color=blue]
                  > interface natively from C, and so is likely to be more efficient[/color]
                  (untested).[color=blue]
                  >[color=green]
                  > >$sql1="selec t * from inventory where item_id = '$item_id'";[/color]
                  >
                  > *bash over head - this is some thing you must never do*
                  >
                  > MySQL has the flaw that you have to stuff values into SQL statements,[/color]
                  mixing[color=blue]
                  > DATA with SQL. This is Bad. Sensible databases use BIND VARIABLES,
                  > alternatively named PLACEHOLDERS.
                  >
                  > A properly rewritten version of above would look like:
                  >
                  > select * from inventory where item_id = :item_id
                  >
                  > Or:
                  >
                  > select * from inventory where item_id = ?
                  >
                  > Depending on what interface and database you're using.
                  >
                  > (Actually you'd probably also want to lose the '*' and replace it with a
                  > proper column list to insulate you from schema changes).
                  >
                  > Stuffing data into literal SQL causes several problems:
                  >
                  > (1) SQL injection attacks. You forget to escape quotes, and suddenly data
                  > becomes executed SQL. This is a severe security problem.
                  >
                  > (2) Oracle, and other databases, cache the execution plans of SQL. The[/color]
                  first[color=blue]
                  > time it comes across a new statement, it works out the best way to execute[/color]
                  it.[color=blue]
                  > Since it has a decent optimiser, this is a relatively expensive operation,[/color]
                  as[color=blue]
                  > it does a fair bit of work to find out what's really the best way to do it[/color]
                  from[color=blue]
                  > the many access paths it has available. But it caches this in memory, so[/color]
                  when[color=blue]
                  > you execute it again later (you rarely only run something *once*) it[/color]
                  doesn't[color=blue]
                  > have to go through the parsing and optimisation steps, it already knows[/color]
                  HOW to[color=blue]
                  > execute it, it just has to plug in the values.
                  >
                  > But if you put literal values in an SQL statement, the text of the[/color]
                  statement[color=blue]
                  > is different every time you execute it. So it can't use the cached plan,[/color]
                  and[color=blue]
                  > has to re-parse it, and do all the optimisation again. This causes[/color]
                  excessive[color=blue]
                  > "hard-parsing" and cripples your database.
                  >
                  > If you have bind variables/placeholders, then the execution plan is the[/color]
                  same[color=blue]
                  > (since the SQL is the same) but you plug in the _data_ later.
                  >
                  > $sql = 'select * from inventory where item_id = :item_id';
                  > $stmt1 = OCIParse($conn, $sql1);
                  >
                  > if (!$stmt1) {
                  > // handle the error, with reference to OCIError()
                  > }
                  >
                  > OCIBindByName($ stmt, ':item_id', &$item_id, -1);
                  >[color=green]
                  > >OCIDefineByNam e($stmt1, "item_id", &$id);
                  > >OCIDefineByNam e($stmt1, "item_desc" , &$desc);
                  > >OCIExecute($st mt1);
                  > >while(OCIFetch ($stmt1)){
                  > > echo $id.' - '.$desc.'<br>';
                  > >}[/color]
                  >
                  > You have a choice between using defines, or using OCIFetchInto which[/color]
                  wraps[color=blue]
                  > this up for you and gives you PHP arrays. It's up to you. Read the
                  > documentation, make your own decision.
                  >[color=green]
                  > >OCILogoff($con n);
                  > >
                  > >---------------------------------------------------
                  > >2) What are the benefits to using ADODB??
                  > >---------------------------------------------------[/color]
                  >
                  > ADODB has a manual. The very first chapter of the manual gives reasons[/color]
                  why you[color=blue]
                  > might want to use ADODB.
                  >
                  > Geoff already gave you a reason why; it hides you from some of the OCI8
                  > interface, which is quite low-level, and gives you a higher-level uniform
                  > interface that works for many different types of database.
                  >[color=green][color=darkred]
                  > >> That link you provided does not work![/color]
                  > >
                  > >An example why not to top-post. Put you responses in-line.[/color]
                  >
                  > You (Philip) didn't write this. But because you're still top-posting[/color]
                  (look it[color=blue]
                  > up on Google) and further you're not quoting correctly, Geoff's reply is
                  > attributed to you in your reply.
                  >
                  > --
                  > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                  > <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>[/color]


                  Comment

                  • Andy Hassall

                    #10
                    Re: PHP/Oracle - Pulling data into array

                    On Wed, 18 Feb 2004 12:50:34 -0500, "Philip D Heady" <pdheady@comcas t.net>
                    wrote:
                    [color=blue]
                    > "You have a choice between using defines, or using OCIFetchInto which wraps
                    >this up for you and gives you PHP arrays. It's up to you. Read the
                    >documentatio n, make your own decision."
                    >
                    >From your poor explanation, I would conclude that I three choices not two.
                    >So which is it?
                    >
                    >OCIDefineByNam e, OCIBindByName, or using OCIFetchInto
                    >
                    >Perhaps you could be a little clearer.[/color]

                    You have (at least) four choices for fetching data, but OCIBindByName is not
                    one.

                    OCIDefineByName is for binding PHP variables to columns to get data out of a
                    result set; each time you OCIFetch a row, the variables get set to the
                    corresponding columns of the result set.

                    Or, OCIResult fetches the value of one column in the 'current' row.

                    Or, OCIFetchInto fetches a single row as an array.

                    Or, OCIFetchStateme nt, which fetches an array (one entry per row) of arrays
                    (one entry per column of that row) - i.e. the entire result set in one call.


                    OCIBindByName is for binding data IN to placeholders, not for fetching data.

                    For a given SQL statement, you follow this cycle:

                    Prepare: OCIParse - you do this once.

                    Loop: (for each different set of values you want to query with):
                    Bind: OCIBindByName - for each bind variable/placeholder in the query.
                    [ Define: Optionally bind PHP variables to result set columns. ]
                    Execute: OCIExecute - begin execution of the statement.

                    Fetch: Either:
                    Loop: (for each row in the result set)
                    Then either:
                    OCIFetch (values go into previously OCIDefineByName 'd variables
                    or use OCIResult)
                    or OCIFetchInto (PHP fetches the row into a PHP array)
                    End loop (when no more rows)
                    Or: OCIFetchStateme nt: Fetch all rows in one go

                    End loop (when you're finished with the cursor)

                    So for 'select a, b, c from inventory where d = :d' you would have:

                    * One call to OCIBindByName to bind a value to the :d placeholder.
                    * [Optionally] Three calls to OCIDefineByName to bind PHP variables to receive
                    values for a, b and c when fetching.

                    Any clearer?

                    --
                    Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                    <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

                    Comment

                    Working...