Reputation: 61
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;
}
Upvotes: 1
Views: 643
Reputation: 11
The use of the function odbc_fetch_array ($pstm, $i) with the second parameter starting with zero causes the problem. When you use 0, and the second time 1, it will give you the same result. You could use a while-loop instead of a for-loop and don't use the second parameter.
while ($row = odbc_fetch_array($result))
{
// do something
}
Upvotes: 0
Reputation: 7019
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.
Upvotes: 1