Search code examples
phpsqldatabaseodbc

odbc query returns the first row twice then returns the rest normally


I am querying a database with an odbc connection through php. When I query the db it returns the first row twice and then the rest of the rows the correct amount of times.

Example query:

$stm = SELECT[sUsername] FROM [dbo].[Abilis].[Users] WHERE sUsername = ?;
$pstm = odbc_prepare($conn, $stm);
$exc = odbc_execute($query, array($Username));

I have also tried using DISTINCT but that has not worked either.

EDIT:

for($i=0; $i<odbc_num_rows($pstm);$i++){

            $row = odbc_fetch_array($pstm, $i);
            if($row['OnCreditHold'] == '1'){
              $out = '<button style="color:red;margin:0 auto;" class="btn" onclick="'.'window.location.href='."'information.php?info=".$row['Account_no'];
              $out .= "'".'">'.$row['Name'].'</br>';
              $out .= $row['Del_ad1'].'</button>';
            }
            else{
              $out = '<button class="btn" style="margin: 0 auto;" onclick="'.'window.location.href='."'information.php?info=".$row['Account_no'];
              $out .= "'".'">'.$row['Name'].'</br>';
              $out .= $row['Del_ad1'].'</button>';

            }
            echo $out;
          }

Solution

  • You have checked the query result is good outside of this app -- good for you. That means the problem is your loop structure, or maybe your method of getting the data such as odbc_fetch_array().

    I ran into this problem once, and I can't recall the solution. I had to try alternative methods to isolate the cause.

    For example, instead of for (), try foreach ($elems as $elem) { ... }.

    Of course, simplify all the other aspects while you are trouble-shooting. For example, remove the if() structure.