0

I have this php case executed when called by a function in my main page. When I type my variables like this: $tutorReasonString = "I'm fine and working." and $clientReasonString = "I'm fine too, thanks for asking", nothing is wrong here. But when I try to fetch them with pg_fetch_array(), I get NULL values returned.

Note that tutorabsence and clientabsence tables are not connected with any kind of SQL trigger, I only assign them exact same id in another execution which is retrieved from a row of another table with php.

case "absencePc":
                
                $id = array_search(post("id"), $_SESSION["keyhash"]);
                
                $tutorAbsSql = "SELECT reason FROM tutorabsence WHERE id='$id'";
                $tutorReason = pg_query($tutorAbsSql);
                if(pg_fetch_row($tutorReason) == NULL) {
                    
                    $tutorStatus = "0";
                    $tutorReasonString = "";
                    
                } else {
                    
                    $tutorStatus = "1";
                //  $tutorReasonString = "I'm working"; This works  
                
                // This returns null.
                    $tutorReasonFetch = pg_fetch_array($tutorReason);
                    $tutorReasonString = $tutorReasonFetch["reason"];
                    
                   
                };
                
                
                $clientAbsSql = "SELECT reason FROM clientabsence WHERE id='$id'";
                $clientReason = pg_query($clientAbsSql);
                if(pg_fetch_row($clientReason) == NULL) {
                    
                    $clientStatus = "0";
                    $clientReasonString = "";

         
                } else {
                    
                                        
                    $clientStatus = "1";  
                //  $clientReasonString = "I'm working";  This works
                
                // This returns null.
                    $clientReasonFetch = pg_fetch_array($clientReason);
                    $clientReasonString = $clientReasonFetch["reason"];
                   
                    
                };
                
         $response[] = array("tutorReason" => $tutorReasonString,
                             "clientReason" => $clientReasonString,
                             "tutorStatus" => $tutorStatus,
                              "clientStatus" => $clientStatus);
                
                echo json_encode($response);
                
break;

4
  • Ever bothered to check the docs on "when" it can return "null"? I mean, it seems silly, but it would be my first step to take too understand a functions behaviour. Commented Feb 18, 2017 at 22:36
  • Obviously, I checked that exact page several times before I bothered to ask a question here. There would be things I've missed though. Commented Feb 18, 2017 at 22:39
  • Do you expect multiple reason per $id, or is it just one? Commented Feb 18, 2017 at 22:44
  • I expect only one reason per table with provided $id Commented Feb 18, 2017 at 22:47

1 Answer 1

2

The problem you're having is that you're fetching twice from 1 result:

$tutorAbsSql = "SELECT reason FROM tutorabsence WHERE id='$id'";
$tutorReason = pg_query($tutorAbsSql);

if(($row = pg_fetch_row($tutorReason)) != false){
  $tutorStatus = "1";
  $tutorReasonString = $row[0];
  print_r($row);
} else {
  $tutorStatus = "0";
  $tutorReasonString = ""; 
}

It internally moves the result pointer forward, then pg_fetch_array() is then fetching for the next value, which does not exist.

Sign up to request clarification or add additional context in comments.

3 Comments

I changed the code the way you suggested it. Strangely, I get undefined index error now, but results are shown in print_r($row). Here is the screenshot.
I already updated the code, in PostgreSQL it fetches by num, not an assoc array. So its $tutorReasonString = $row[0];
'It internally moves the result pointer forward'. Is there a way to stop this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.