Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie SQLSRV_Query 'Null Resource' Question

Status
Not open for further replies.

Mav3000

Technical User
Jun 20, 2005
113
GB
I am trying to query a mysql database using PHP.

I have a function in a seperate PHP (PHP File A) file which I call to connect to the database in PHP File B:

PHP File A:
===========

PHP:
function RS_Connect()
{
// Database Connection Settings
$RS_Server = "RSDB";
$RS_Settings = array("UID"=>"ReadOnly", "PWD"=>"password", "Database"=>"RSDB");
$RS_Connection = sqlsrv_connect($RS_Server, $RS_Settings);

if($RS_Connection)
{
  echo "Connection Established.\n";
	}
	else
	{
		 echo "Connection Could Not Be Established.\n";
		 die( print_r( sqlsrv_errors(), true));
	}

}

PHP File B:
===========

PHP:
// Connect To SQL Database
RS_Connect();

// SQL Query
$Query = "SELECT tblMobUsers.UserID, tblMobUsers.Fullname ";
$Query.= "FROM tblMobUsers";

// Execute SQL Query
$Query_Results = sqlsrv_query($RS_Connection,$Query);

// Display Number of Results
$Query_Results_Count = sqlsrv_num_rows($Query_Results);

My problem is that when I open PHP File B, I get the error:

"Warning: sqlsrv_query() expects parameter 1 to be resource, null given"

What am I doing wrong here? If I incorporate the code from A into B it connects and queries fine, but I want this in a separate file so I can control the connection details in a single location.

I include the PHP File A in the Header of PHP File B, and the 'RS Connect' Function returns 'Connection Established' right above the Warning error.

Any help appreciated!
 
What am I doing wrong here? If I incorporate the code from A into B it connects and queries fine, but I want this in a separate file so I can control the connection details in a single location.

How are you incorporating it when it runs? Are you using the same function?

As it stands your connection variable $RS_Connection only exists within the scope of your function. So when you attempt to use it, it technically has not been defined outside of your function.


You can return the resource variable or make it global.


Code:
function RS_Connect()
{
// Database Connection Settings
$RS_Server = "RSDB";
$RS_Settings = array("UID"=>"ReadOnly", "PWD"=>"password", "Database"=>"RSDB");
$RS_Connection = sqlsrv_connect($RS_Server, $RS_Settings);

if($RS_Connection)
{
  echo "Connection Established.\n";
	}
	else
	{
		 echo "Connection Could Not Be Established.\n";
		 die( print_r( sqlsrv_errors(), true));
	}
[indent][b][COLOR=#A40000]return $RS_Connection;[/color][/b][/indent]
}


...

[b][COLOR=#A40000]$returned_RS_Connection = RS_Connect();[/color][/b]


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Phil, That has sorted it and now works perfectly. Thank you very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top