Log in

View Full Version : connecting php to ms sql server 2005



sukanya.paul
03-11-2008, 08:46 AM
hi i h to connect php to ms sql server 2005 and i hv no clue how to do it.. can some1 pls help.. i'll be very greatful
thank in advance..Suk

codeexploiter
03-11-2008, 09:05 AM
http://msdn2.microsoft.com/en-us/library/bb264561.aspx
http://in.php.net/mssql

sukanya.paul
03-11-2008, 09:08 AM
well i found those links using google but could not really follow them. i'm pretty new to php n was using mysql till now.. can u help with anythin more?please??

codeexploiter
03-11-2008, 10:07 AM
Have you got the following code in which they are demonstrating the connection and retrieval data using PHP from an MS SQL DB



<?php
/*Connect to the local server using Windows authentication and specify
the AdventureWorks database as the database in use. */
$serverName = "(local)";
$connectionInfo = array( "Database"=>"AdventureWorks");
if( !($conn = sqlsrv_connect( $serverName, $connectionInfo)))
{
echo "Could not connect.\n";
die( print_r( sqlsrv_errors(), true));
}

/* Set up the T-SQL query. */
$tsql = "SELECT ReviewerName,
ReviewDate,
Rating,
Comments
FROM Production.ProductReview
WHERE ProductID = ?
ORDER BY ReviewDate DESC";

/* Set the parameter value. */
$productID = 709;
$params = array( $productID);

/* Execute the query. */
if( !($stmt = sqlsrv_query($conn, $tsql, $params)))
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}

/* Retrieve and display the data. The first and third fields are
retrieved according to their default types, strings. The second field
is retrieved as a string with 8-bit character encoding. The fourth
field is retrieved as a stream with 8-bit character encoding.*/
while ( sqlsrv_fetch( $stmt))
{
echo "Name: ".sqlsrv_get_field( $stmt, 1 )."\n";
echo "Date: ".sqlsrv_get_field( $stmt, 2,
SQLSRV_PHPTYPE_STRING( SQLSRV_ENC_CHAR))."\n";
echo "Rating: ".sqlsrv_get_field( $stmt, 3 )."\n";
echo "Comments: ";
$comments = sqlsrv_get_field( $stmt, 4,
SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_CHAR));
fpassthru( $comments);
echo "\n";
}

/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>

sukanya.paul
03-12-2008, 02:10 AM
its giving me the following error:

Fatal error: Call to undefined function sqlsrv_connect()

i think this code is for sql server 2000. i need to connect to 2005