Link to home
Start Free TrialLog in
Avatar of peter Ojeda
peter Ojeda

asked on

Select from sql by results from textbox

Hi experts, I have a table in my database with the columns MONTH_ and YEAR_. I am trying to display this by using a select statement that the WHERE field comes from textbox inputs. My idea is SELECT * FROM D_LINE_MONTH DATA WHERE YEAR=(TEXTBOX RESULTS) AND YEAR = (TEXTBOX RESULTS). My two text boxes are also MONTH_ and YEAR_. I keep getting the error "Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given "
<?php
   ini_set('display_errors', 1);
   error_reporting(~0);
	
	$serverName = "vnmwhatsup\PWRBI_REPORTS";
	$userName = "appuser";
	$userPassword = 'appuser';
	$dbName = "ENERGY";

  

   $strID = null;

   if(isset($_GET["ID"]))
   {
	   $strID = $_GET["ID"];
   }
  
   $connectionInfo = array("Database"=>$dbName, "UID"=>$userName, "PWD"=>$userPassword, "MultipleActiveResultSets"=>true);

   $conn = sqlsrv_connect( $serverName, $connectionInfo);

   if( $conn === false ) {
      die( print_r( sqlsrv_errors(), true));
   }

	$stmt = "SELECT * FROM D_LINE_MONTH_DATA WHERE YEAR_ =  + 'YEAR_' AND MONTH_ = + 'MONTH_'";
	$params = array($strID);

	$query = sqlsrv_query( $conn, $stmt, $params);

	$result = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC);

?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peter Ojeda
peter Ojeda

ASKER

Thanks Julian that helped me find and remove the error from my query. I still am not able to execute the query on textbox inputs though. When I manually set YEAR_=2016 AND MONTH_=4 I am able to select the data, but I am trying to have YEAR_ and MONTH_ come from textbox inputs.
Almost all PHP functions return values.  Your PHP script can test these values to see what the function did.  Check the code examples in the man pages.
http://php.net/manual/en/function.sqlsrv-connect.php
http://php.net/manual/en/function.sqlsrv-query.php
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi thank you both for your help. I was able to get it to work by setting and concatenating the input variables and it works perfectly.
You are welcome.