php - Using Inner Join and mysql_num_rows() is Always returning 1 less row -


i checked throught existing topics. have fix problem know not right fix , i'm more interested making work right, creating workaround it.

i have project have 3 tables, diagnosis, visits, , treatments. people come in visit, treatment, , treatment diagnosis.

for displaying information on page, want show patient's diagnosis, show time came in visit, visit info can clicked on show treatment info.

to made function in php:

<? function returntandv($dxid){     include("db.info.php");      $query = sprintf("select treatments.*,visits.* treatments left join visits on     treatments.tid = visits.tid treatments.dxid = '%s' order visits.dos desc",     mysql_real_escape_string($dxid));      $result = mysql_query($query) or die("failed because: ".mysql_error());      $num = mysql_num_rows($result);     for($i = 0; $i <= $num; ++$i) {         $v[$i] = mysql_fetch_array($result mysql_assoc);         ++$i;     }      return $v; } ?> 

the function works , display want of rows both treatments , visits 1 large assoc. array problem returns 1 less row in database , i'm not sure why. there 3 rows total, msql_num_rows() show 2. work around has been add 1 ($num = mysql_num_rows($result)+1;) rather have correct.

this section looks suspicious me:

for($i = 0; $i <= $num; ++$i) {     $v[$i] = mysql_fetch_array($result mysql_assoc);     ++$i; } 
  1. you're incrementing i twice
  2. you're going $i <= $num when want $i < $num

this combination may why you're getting unexpected results. basically, have 3 rows, you're asking rows 0 , 2 (skipping row 1).


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -