Solved

sqlsrv_get_field()

Posted on 2010-11-22
4
2,197 Views
Last Modified: 2013-12-12
I'm needing a little help here. Would anyone know why my sqlsrv_get_field($myData,0) would be complaining with "Warning: sqlsrv_get_field() expects parameter 1 to be resource, boolean given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\TMCTranscriptionViewer\downloadfile.php on line 43  "

Here's the output for $myData,
$myData = bool(true)
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);
date_default_timezone_set('America/Tegucigalpa') ;
$REPORTID = $_GET['REPORTID'];

echo 'ReportID = '.$REPORTID.'<br>';
if(!is_numeric($REPORTID))
    die("Invalid REPORTID specified. REPORTID = " . $REPORTID);

$serverName = "TEXPARRPT";
$connectionInfo = array( "Database"=>"MEDQUIST","UID"=>"xx","PWD"=>"xxxxx");

/* Connect */
$conn2 = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn2 ){
//    echo "<br>Connection ready to go!<br>";
}
else
{
     echo "<br>Could not connect to ".$serverName ."." . $connectionInfo["Database"]."<br><br>";
     die( print_r( sqlsrv_errors(), true));
}

$dbQuery = "SELECT MIMETYPE, CONTENT
            FROM REPORTCONTENTT
            INNER JOIN REPORT_DESCT ON REPORTCONTENTT.REPORTID = REPORT_DESCT.REPORTID
            WHERE REPORTCONTENTT.REPORTID = ".$REPORTID;
$result = sqlsrv_query($conn2, $dbQuery);
// Note: I do get results from sqlsrv_query() above.
if($result){
//    echo '<br>Got results from sqlsrv_query()!<br><br>';
    $myData = sqlsrv_fetch($result);
// Note: I do get results from sqlsrv_fetch() above.
    if($myData){
        echo '<br>sqlsrv_fetch() suceeded! sqlsrv_fetch($result) = '.$myData.'<br>';
//        var_dump($myData);
        }
    else{
        echo '<br>Error with sqlsrv_fetch($result). $myData = <br>';
        var_dump($result);
        }
    $fileType = sqlsrv_get_field($myData,0);
// Warning: sqlsrv_get_field() expects parameter 1 to be resource, boolean given...
    if($fileType){
        echo '<br>sqlsrv_get_field() suceeded!<br>FileType = '.$fileType;
        }
    else{
        echo '<br>sqlsrv_get_field($myData,0) failed. $myData = ';
        var_dump($myData);
        echo '<br><br>';
        print_r( sqlsrv_errors(), true);
        }


    }
else{
    echo '<br>Something happened with sqlsrv_query()<br>';
    die( print_r( sqlsrv_errors(), true));
    }


////    echo $fileContent;
//    }
//else
//    {
//    echo "Record doesn't exist.";
//    }
sqlsrv_free_stmt($result);
sqlsrv_close($conn2);
?>

Open in new window

0
Comment
Question by:ajfjr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 34191731
It seems sqlsrv_get_field() expects to find something like a string identifying a MIME file type and it find a boolean value. If this is the case, then I would look at sqlsrv_fetch($result) function which return a boolean value and not a string. Can you post the code for sqlsrv_fetch()?
0
 
LVL 5

Accepted Solution

by:
wmadrid1 earned 500 total points
ID: 34192271
Try this:

$f = 0;
$type = sqlsrv_field_metadata($result);
$fileType = ($type[$f]['Type'] == SQLSRV_SQLTYPE_FLOAT) ? sqlsrv_get_field($result, $f) : sqlsrv_get_field($result, $f, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)); 

Open in new window

//$fileType = sqlsrv_get_field($myData,0);

I am supposing $f is the first SELECT field
0
 

Author Comment

by:ajfjr
ID: 34192438
This was interesting and it works! Could you explain what's going on so a PHP newbie like me can understand what's going on?

Thank You!
0
 
LVL 5

Expert Comment

by:wmadrid1
ID: 34192716
Basically your error was the first paramater of function
$fileType = sqlsrv_get_field($myData,0);
the correct line is a valid resource
$fileType = sqlsrv_get_field($result,0);

The rest  is a good validating returning data types
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question