Jump to content

ODBC Query Problem - Undefined index


will35010

Recommended Posts

I have this code:

$sql = "SELECT adpat, adptnm, addate, adtime, adddsc, disdt, distm, drname, adptyp, ptprvtyp FROM hma711.zadpatnmf 
JOIN hma711.addocrmf on adamdr = drno WHERE addate BETWEEN '20111115' AND '20111115' AND adpat BETWEEN '2000000' AND '2999999'";

$execute = odbc_exec($conn, $sql);

$num = odbc_num_rows($execute);
echo $num;

while($row = odbc_fetch_array($execute)){
    echo $row['adpat'];
} 

It gets an error on this line: echo $row['adpat'];

 

The error is Notice: Undefined index: adpat in C:\xampp\htdocs\erboard\test.php on line 29

 

The query works directly on the AS400 I'm pulling from and the $num is being populated with the correct number of records.

 

This is the first time I have ever used php with odbc so I'm not sure what I'm missing. Any help would be greatly appreciated. Thanks!

Link to comment
Share on other sites

odbc_fetch_array() may not (ever?) include column names in the array. Assume it's just a straight list with numeric keys.

 

You could fairly easily build your own function to return an associate array by using odbc_field_name.

 

I tried this without any luck

for($i=1;$row=odbc_fetch_row($execute,$i);$i++) {    
    echo $row[0];
}

Link to comment
Share on other sites

I have it returning the results with this:

$sql = "SELECT adpat, adptnm, addate, adtime, adddsc, disdt, distm, drname, adptyp, ptprvtyp FROM hma711.zadpatnmf 
JOIN hma711.addocrmf on adamdr = drno WHERE addate BETWEEN '20111115' AND '20111115' AND adpat BETWEEN '2000000' AND '2999999'";

$execute = odbc_exec($conn, $sql);

$count = odbc_num_rows($execute);
echo $count."</br>";
$i = 1;
while($i<=$count)
  {
  $pat_num = odbc_result($execute, "adpat");
  echo $pat_num;
  $pat_nam = odbc_result($execute, "adptnm");
  echo $pat_nam."</br>";
  $i++;
  }

 

But it repeatedly echos on the first row of data for all records returned.

Link to comment
Share on other sites

I got it working with this:

$i = 1;
while($row = odbc_fetch_array($execute, $i)){
    $pat_num = odbc_result($execute, "adpat");
    echo $pat_num;
    $pat_nam = odbc_result($execute, "adptnm");
    echo $pat_nam."</br>";
    $i++;
}

 

AS400 DB2 is no fun compared to mysql.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.