Page 1 of 1

Connect PHP to SQL Server 2008 R2

PostPosted: 02. March 2012 00:21
by ruxee
I add the step-by-step guide to make it in
http://www.forosdelweb.com/f18/conectarse-ms-sql-server-2008-desde-php-979188/.

Is in spanish but if you want it in english let me know and I post it here.

Regards

Re: Connect PHP to SQL Server 2008 R2

PostPosted: 25. March 2012 06:41
by unWing
I've followed your guide correctly and got connected to the server, but when i try to retrieve the records nothing happen. it return blank. Here is the code Im trying to test.

<?php
$serverName = "MyServer";
$connectionInfo = array( "Database"=>"MyDBS");

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

if( $conn === false )
{
echo "Unable to connect.</br>";
die( print_r( sqlsrv_errors(), true));
}
else
{
echo "Connection established.</br>";
}

$tsql = "SELECT * FROM Personnel";
$stmt = sqlsrv_query($conn, $tsql);

if( $stmt === false )
{
echo "Error in executing query.</br>";
die( print_r( sqlsrv_errors(), true));
}

if (sqlsrv_num_rows($stmt) > 0 ) //not working, returned blank
{
echo "<table cellpadding=10 border=1>";
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";//instead of numeric use the field name $row["CompanyNum"]
echo "<td>".$row[1]."</td>";//$row["EmployeeNum"]
echo "</tr>";
}
echo "</table>";
}
else {
echo "No rows found!";
}

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);

?>

Re: Connect PHP to SQL Server 2008 R2

PostPosted: 26. March 2012 02:49
by unWing
I've corrected my previous post and its working now. Thanks