Jump to content

Recommended Posts

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:

array1.jpg

 

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:

array2.jpg

 

Do you all have any suggestions? Thanks a ton for looking at this.

Link to comment
https://forums.phpfreaks.com/topic/270328-problem-echoing-sqlsrv-results/
Share on other sites

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 />";
}

error.jpg

 

@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:

error2.jpg

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 by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.