problem with printing query result

Discussion in 'PHP' started by steyr, Aug 4, 2007.

  1. #1
    Hy!

    I have a problem with printing query results. I thought it will work with a simple echo statement, but it does not.
    When I run the following code:
    
    
    if ($c = @oci_connect("username","password","xe"))
      {
           $s = oci_parse($c,"SELECT '$text'
      FROM text_web
      where
      ID = '1'");
    
        
        oci_execute($s,OCI_DEFAULT);
     echo $s;
     }
    
    Code (markup):
    As result I get: Resource id #2

    May somebody tell me where I went wrong here!

    Thanks for help!

    steyr
     
    steyr, Aug 4, 2007 IP
  2. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The variable '$s' would store the statement resource, which is why you get "Resource ID #n" when you try to print it out.
    To get the actual row/data, you'll want to use one of the "oci_fetch_*" functions.

    For example:
    
    oci_execute($s, OCI_DEFAULT);
    $row = oci_fetch_array($s);
    echo $row['field'];
    
    PHP:
    Where 'field' is the name of the table field you're trying to output.
    "oci_fetch_array" returns both numeric and associative arrays. There's also "oci_fetch_object" and others, to return the data in different formats.
     
  3. steyr

    steyr Guest

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hy!

    Thanks for reply! I tried what you posted and some other variants and I always got stuck at with the same error massage:

    Notice: Undefined index: english in C:\Programme\Zend\Apache2\htdocs\test.php on line 34

    what defines the table field is in my case a variable!

    here is an example

    
       
       if($language == 'en'){
       $text = 'english';
       }
       else if ($language == 'de') {
       
       $text= 'german';
       } 
       
       if ($c = @oci_connect("username","password","xe"))
      {
        
        $s = oci_parse($c,"SELECT '$text'
      FROM text_web
      WHERE
      ID = '1'");   
        oci_execute($s,OCI_DEFAULT);
     oci_execute($s, OCI_DEFAULT);
    $row = oci_fetch_array($s);
    echo $row[$text];
    }
    
    Code (markup):
     
    steyr, Aug 4, 2007 IP
  4. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The second "oci_execute" you have is unneeded.
    Additionally, you may want to change your query, to "SELECT $test" or "SELECT {$test}" instead.

    Your edited code:
    
    if($language == 'en'){
       $text = 'english';
       }
       else if ($language == 'de') {
       
       $text= 'german';
       } 
       
       if ($c = @oci_connect("username","password","xe"))
      {
        
        $s = oci_parse($c,"SELECT {$text}
      FROM text_web
      WHERE
      ID = '1'");
    
     oci_execute($s, OCI_DEFAULT);
    
    $row = oci_fetch_array($s);
    echo $row[$text];
    }
    
    PHP:
    Try that, and if it doesn't work, change "echo $row[$text];" to "var_dump($row);" and post the results.
     
  5. steyr

    steyr Guest

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hello!

    Thanks for help!!!

    I tried as you posted, but the result is still the same. When I use "var_dump($row);" I get the correct result but in the array form:

    array(2) { [0]=> string(14) "this is a test" ["ENGLISH"]=> string(14) "this is a test" }
     
    steyr, Aug 4, 2007 IP
  6. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Note that higher-end databases like Oracle will uppercase all field names when returning results. Unfortunately I don't work with these database servers very often.

    If you want to continue using the field names in $text, I'd recommend changing your initial "if" statements to look as such:
    
    if ($language == 'en') {
         $text = 'ENGLISH';
    }
    elseif ($language == 'de') {
         $text= 'GERMAN';
    }
    
    PHP:
    Otherwise, as you can see from the array, you can also access your desired result using "$row[0]" instead of "$row[$text]", since you have only one field being returned.
     
  7. nabiha

    nabiha Peon

    Messages:
    111
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Its better that you use $row[0] for slightly better performance.
     
    nabiha, Aug 4, 2007 IP