imprint Posted May 7, 2014 Share Posted May 7, 2014 I have the following code which prints results from a sqlsrv_fetch_array() using a while loop. For one segment, however, I only need the first $value of the $key=>$value pair, and obviously with the code written this way it returns all rows. I have an idea of an approach, and I'm wondering if I'm headed in the right direction or if anyone has experience with this that they can share. So, during the while loop, I am pushing the elements of the sqlsrv_fetch_array() into a separate array. So I see two potential options: 1. Print from the $actcontacts[] array rather than from the sqlsrv fetch array. 2. During the while loop, rather than pushing anything into a new array, build a conditional that says to only print the record if $row['ENDTIME'] is in position $row[0]. I'm leaning toward #2, but I'm not quite sure how to get there...and if there is a better way under door #3, I would like to try that. while ($row = sqlsrv_fetch_array( $stmt_act, SQLSRV_FETCH_ASSOC) ) { $actcontacts[] = "N/A" . "," . $row['COMPANYNAME'] . "," . $row['CITY'] . "," . $row['STATE'] . "," . $row['CUST_Salesman_124705300'] . "," . $row['ENDTIME'] . "," . "ACT"; // I have done a print_r() on $actcontacts and the data is correct. $endtime[] = $row['ENDTIME']; echo "<tr>"; echo "<td>" . "<i>N/A</i>" . "</td>"; echo "<td>" . $row['COMPANYNAME'] . "</td>"; echo "<td>" . $row['CITY'] . "</td>"; echo "<td>" . $row['STATE'] . "</td>"; echo "<td>" . $row['CUST_Salesman_124705300'] . "</td>"; echo "<td>" . $row['ENDTIME'] . "</td>"; // This is the bugger that is printing eleventy records. It may be one customer, but there are always multiple contact/history entries. echo "<td>" . "ACT" . "</td>"; echo "</tr>"; } Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted May 8, 2014 Solution Share Posted May 8, 2014 Are you able to adjust the query so it only fetches one result? You could do this with the LIMIT clause. https://www.google.com/search?q=sql+limit Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted May 8, 2014 Share Posted May 8, 2014 (edited) So you want the value of $row['ENDTIME'] to be output only once for the first row of the returned result set and not for the 2nd, 3rd, 4th etc...rows? Then what you need is an if at the start of the loop while ($row = sqlsrv_fetch_array( $stmt_act, SQLSRV_FETCH_ASSOC) ) { $endtime = !isset($endtime) ? $row['ENDTIME'] : ''; $actcontacts[] = "N/A" . "," . $row['COMPANYNAME'] . "," . $row['CITY'] . "," . $row['STATE'] . "," . $row['CUST_Salesman_124705300'] . "," . $endtime . "," . "ACT"; echo "<tr>"; echo "<td>" . "<i>N/A</i>" . "</td>"; echo "<td>" . $row['COMPANYNAME'] . "</td>"; echo "<td>" . $row['CITY'] . "</td>"; echo "<td>" . $row['STATE'] . "</td>"; echo "<td>" . $row['CUST_Salesman_124705300'] . "</td>"; echo "<td>" . $endtime . "</td>"; // should only be output for the first row echo "<td>" . "ACT" . "</td>"; echo "</tr>"; } Edited May 8, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
imprint Posted May 8, 2014 Author Share Posted May 8, 2014 Ch0cu3r, that is what I was thinking and was trying to see if I was on the right path, thanks! cyberRobot, DOH! I can't believe I didn't think of that. So simple and will speed things up as well. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 9, 2014 Share Posted May 9, 2014 DOH! I can't believe I didn't think of that. So simple and will speed things up as well. I've been there. I tend to go straight to PHP for solutions. I've been learning, however, that MySQL has a lot to offer. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.