Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need to set a SQL connection string based on a PHP test code used to test the database

here is the string i have made(does not work)

"Data Source=something.com:3631;Initial Catalog=database;User Id=Neo;Password=qwerty;"

here is the test php file, Did i make something wrong?
note the php file does work

PHP
<?php

$dbname = 'database';
$server = 'something.com:3631';
$username = 'Neo';
$password = 'qwerty';

$sqlquery = "SELECT * FROM sometable ;";
//$sqlquery = "INSERT INTO sometable (fSite, Area, fColumn, Shelf, Item, Quantity) VALUES ('600','FA','8012','3','577300','200');";

$data = mssqlExec($sqlquery, $dbname, $server, $username, $password, $type=MSSQL_ASSOC);

print_r($data);



/**
* Generic Sybase connection to MS SQL using Linux box.
*
* @param int $type 	Can be MSSQL_ASSOC, MSSQL_NUM or MSSQL_BOTH These are constants (numbers) so do not put quotes around them.
* @param string $sqlquery 	the sql query, ensure no semicolon on the end and double quotes with backslashes for name with spaces
* @param string $dbname	name of the database
* @param string $server	name of the server it is hosted on
* @param string $username	username associated with the database
* @param string $password	passsword for that user
* @param global $db_error	contains relevant error message for debugging
*
* @returns array $results	Returns a mutildimensional array with rows an array of fields and values 
*				(row0=>(field0=>value0,field1=>value1),row1=>(field0=>value0,filed1=>value1))
*				
* EXAMPLE: mssqlExec("SELECT Field FROM Table", "PROD1", "sql2.network.com", "fred", "pass", MSSQL_BOTH);
*/
function mssqlExec($sqlquery, $dbname, $server, $username, $password, $type=MSSQL_ASSOC)
{
	global $db_error;
	$sqlconnect=mssql_connect($server,$username,$password);
	
	if (!$sqlconnect) {
		$db_error = "Could not Connect to $server"; 
		return FALSE;
	}
	
	$sqldb=mssql_select_db($dbname,$sqlconnect);
	$process=mssql_query($sqlquery);
	
	if(!$process) {
		$db_error = "Query did not return any results";
		return FALSE;
	}
	
	$chk = substr($sqlquery, 0, 6);
	if ($chk == 'SELECT') {
		while ($row=mssql_fetch_array($process,$type)) {
			foreach ($row as $colNum=>$value) {
				$trimmed[$colNum] = trim($value);
			}
			$results[]=$trimmed;
		}
	} else { 
		$results = TRUE; 
	}
	
	mssql_close($sqlconnect);
	return $results;
}
?>

I am using MS Visual studio 2010 and ssql server 2008
Posted

I guess from the articles you've authored that you'd like to connect from C#?

How about these 2. Does this[^] or this[^] help?
 
Share this answer
 
I think is the port its using does a port goes this way
something.com:3631
or this way
something.com,3631
 
Share this answer
 
Comments
enhzflep 2-Jul-12 11:25am    
Hi.
First of all - please don't post comments as answers. Use the "Have a Question or Comment" button - otherwise only you get email notifications and the intended use of the forum is neglected.

Secondly, are you specifying the url as http://www.someaddress.com or as www.someaddress.com (browsers automatically insert the http:// if missing, not sure about mssql)

Finally, yes - typically a port is specified as url:portAddress, though from the example in the first link that uses a IP, the port is specified as url,portAddress.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900