Web Analytics Made Easy -
StatCounter Select in Oracle 10g doubles output - CodingForum

Announcement

Collapse
No announcement yet.

Select in Oracle 10g doubles output

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Select in Oracle 10g doubles output

    The code below connects to an Oracle 10g database, but it produces twice the output of every table


    PHP Code:
    <?php

        
    // CONNECT to the database
    $conn oci_connect('stocks''stocks');
    if (!
    $conn) {
        
    $e oci_error();
        print 
    htmlentities($e['message']);
    exit;
    }

        
    // SELECT and output on the screen
    $query 'SELECT * FROM STOCKS';
    $stid oci_parse($conn$query);
    if (!
    $stid) {
        
    $e oci_error($conn);
        print 
    htmlentities($e['message']);
        exit;
    }
    $r oci_execute($stidOCI_DEFAULT);
    if (!
    $r) {
        
    $e oci_error($stid);
        echo 
    htmlentities($e['message']);
        exit;
    }
    print 
    '<table border="1">';
    while (
    $row oci_fetch_array($stidOCI_RETURN_NULLS)) {
        print 
    '<tr>';
        foreach (
    $row as $item) {
            print 
    '<td>'.($item?htmlentities($item):'&nbsp;').'</td>';
        }
        print 
    '</tr>';
    }
    print 
    '</table>';

    oci_close($conn);
    ?>

    Output

    T100_S T100_S
    T101_S T101_S
    T102_S T102_S

    although it should be

    T100_S
    T101_S
    T102_S

  • #2
    You're telling it to:
    PHP Code:
    print '<td>'.($item?htmlentities($item):'&nbsp;').'</td>'

    Comment


    • #3
      he uses the
      PHP Code:
      $item?htmlentities($item):'&nbsp;' 
      as shorthand for
      PHP Code:
      if ($item){
       echo 
      htmlentities($item);
      }else{
        echo 
      '&nbsp;';

      so that should be correct.
      Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

      Comment


      • #4
        Whoops sorry that's me not quite awake...

        Comment


        • #5
          Correct. Also a simple echo didn't help. Why?
          PHP Code:
          while ($row oci_fetch_array($stidOCI_RETURN_NULLS)) {
              print 
          '<tr>';
              foreach (
          $row as $item) {
                  echo 
          $item;
              }
              print 
          '</tr>';
          }
          print 
          '</table>'
          Of course a direct query to the database does not produce the error.

          Maybe it has to do with the Predefined Constants like 'OCI_DEFAULT'

          Any idea what exactly these are?

          Comment


          • #6
            OK maybe this answer will be a little more thought out and intelligent.

            You are looping through every index in the array created by oci_fetch_array(). the function oci_fetch_array() creates both a numeric index and an associative index, so you are getting two indices with the same value in the loop.

            Use oci_fetch_row() or oci_fetch_assoc() to limit the number of indices to one per row.

            Comment


            • #7
              Fumigator, it's ovbious you are awake now... It worked!

              Actually, output is equal correct with either one of the following
              PHP Code:
              while ($row oci_fetch_row($stid)) { 
              PHP Code:
              while ($row oci_fetch_assoc($stid)) { 
              Thank you.

              Comment

              Working...
              X