mrPickles Posted November 5, 2012 Share Posted November 5, 2012 Hello all, I have a Windows Server 2008 R2 Standard 64-bit server running WAMP 2.2 along with PHP 5.3.13 and a SQL Server Enterprise 64-bit. SQLSRV is registered as a PHP stream using Microsoft Drivers 3.0 for PHP and SQL Server. The connection is made without issue and the query is working as it should, the only problem is that I haven't figured out the correct way to display what is returned by the query. Maybe you all might be able to help me out! Here's a look at a couple things I've been trying. if( $conn ) { echo "Connection established.<br />"; }else{ echo "Connection could not be established.<br />"; die( print_r( sqlsrv_errors(), true)); } $query = "SELECT top (10) docid, sequence, server, share, path, filename "; $query .= "FROM files "; $query .= "WHERE filename like '%2008010639029%'"; $result = sqlsrv_query($conn, $query) or die (sqlsrv_errors()); if(sqlsrv_has_rows($result)){ echo "1 or more rows have been returned<br />";} else {echo "No results were found.<br />";} while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) { print_r ($row); } When that script is run the results of the query are correct it is just displayed like so: Here is the other method that I'm using that gets close. if( $conn ) { echo "Connection established.<br />"; }else{ echo "Connection could not be established.<br />"; die( print_r( sqlsrv_errors(), true)); } $query = "SELECT top (10) docid, sequence, server, share, path, filename "; $query .= "FROM files "; $query .= "WHERE filename like '%2008010639029%'"; $result = sqlsrv_query($conn, $query) or die (sqlsrv_errors()); if(sqlsrv_has_rows($result)){ echo "1 or more rows have been returned<br />";} else {echo "No results were found.<br />";} while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) { $docid = sqlsrv_get_field($result, 0); $sequence = sqlsrv_get_field($result, 1); $server = sqlsrv_get_field($result, 2); $share = sqlsrv_get_field($result, 3); $path = sqlsrv_get_field($result, 4); $filename = sqlsrv_get_field($result, 5); echo "$docid" . "$sequence, " . "$server, " . "$share, " . "$path, " . "$filename, " . '<br />'; } The result is: Do you all have any suggestions? Thanks a ton for looking at this. Quote Link to comment https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/ Share on other sites More sharing options...
Jessica Posted November 5, 2012 Share Posted November 5, 2012 $row is an associative array. If you want to echo docid, you'll echo $row['docid']. etc, etc. Quote Link to comment https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/#findComment-1390394 Share on other sites More sharing options...
Barand Posted November 5, 2012 Share Posted November 5, 2012 As you are requesting an associative array result try $docid = sqlsrv_get_field($result, 'docid'); instead of $docid = sqlsrv_get_field($result, 0); Quote Link to comment https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/#findComment-1390397 Share on other sites More sharing options...
mrPickles Posted November 5, 2012 Author Share Posted November 5, 2012 Wow thank you both for the quick replies. @Jessica - When using that method I encounter the undefined index error and I think I know what you're going to say about that... use isset(). The thing is I don't know exactly what to check means as how I've tried a couple ways without success. The following code generates the error to follow. $query = "SELECT top (10) docid, sequence, server, share, path, filename "; $query .= "FROM files "; $query .= "WHERE filename like '%2008010639029%'"; $result = sqlsrv_query($conn, $query) or die (sqlsrv_errors()); if(sqlsrv_has_rows($result)){ echo "1 or more rows have been returned<br />";} else {echo "No results were found.<br />";} while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) { $docid = sqlsrv_get_field($result, '0'); $sequence = sqlsrv_get_field($result, '1'); $server = sqlsrv_get_field($result, '2'); $share = sqlsrv_get_field($result, '3'); $path = sqlsrv_get_field($result, '4'); $filename = sqlsrv_get_field($result, '5'); echo $row['DOCID'].", ". $row['SEQUENCE'].", ". $row['SERVER'].", ". $row['SHARE'].", ". $row['PATH'].", ". $row['FILENAME']."<br />"; } @Barand - Using your approach: $query = "SELECT top (10) docid, sequence, server, share, path, filename "; $query .= "FROM files "; $query .= "WHERE filename like '%2008010639029%'"; $result = sqlsrv_query($conn, $query) or die (sqlsrv_errors()); if(sqlsrv_has_rows($result)){ echo "1 or more rows have been returned<br />";} else {echo "No results were found.<br />";} while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) { $docid = sqlsrv_get_field($result, 'docid'); $sequence = sqlsrv_get_field($result, 'sequence'); $server = sqlsrv_get_field($result, 'server'); $share = sqlsrv_get_field($result, 'share'); $path = sqlsrv_get_field($result, 'path'); $filename = sqlsrv_get_field($result, 'filename'); echo $row['DOCID'].", ". $row['SEQUENCE'].", ". $row['SERVER'].", ". $row['SHARE'].", ". $row['PATH'].", ". $row['FILENAME']."<br />"; } And yields: Quote Link to comment https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/#findComment-1390404 Share on other sites More sharing options...
Jessica Posted November 5, 2012 Share Posted November 5, 2012 Why are you doing it in all caps when your own print_r shows it clearly in lowercase? Quote Link to comment https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/#findComment-1390405 Share on other sites More sharing options...
Barand Posted November 5, 2012 Share Posted November 5, 2012 And I'm curious to know if echo "$docid, " . "$sequence, " . "$server, " . "$share, " . "$path, " . "$filename, " . '<br />'; worked after trying my suggestion. Quote Link to comment https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/#findComment-1390408 Share on other sites More sharing options...
Jessica Posted November 5, 2012 Share Posted November 5, 2012 (edited) Barand, see his second screenshot. Edited November 5, 2012 by Jessica Quote Link to comment https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/#findComment-1390410 Share on other sites More sharing options...
Barand Posted November 5, 2012 Share Posted November 5, 2012 (edited) Jessica, I know he tried my suggestion but then he does this echo $row['DOCID'].", ". $row['SEQUENCE'].", ". $row['SERVER'].", ". $row['SHARE'].", ". $row['PATH'].", ". $row['FILENAME']."<br />"; EDIT Ah yes - error message. I guess get_field isn't really what he wanted anyway. Haven't used sqlsrv since I retired. Edited November 5, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/#findComment-1390411 Share on other sites More sharing options...
mrPickles Posted November 5, 2012 Author Share Posted November 5, 2012 (edited) That worked... I should have picked up on that but I typically deal with T-SQL which isn't case sensitive. Thank you both for the help, This is a nice community. Edited November 5, 2012 by mrPickles Quote Link to comment https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/#findComment-1390428 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.