Home >Backend Development >PHP Tutorial >PHP connection to sql2008 (Practical)_PHP tutorial
As for the method of php connecting to mssql2008, you can refer to other articles on my blog for introduction
If you operate
We can first read the help file in the downloaded plug-in. It is very detailed but it is in English
You can also get the manual online. The convenient address for the Chinese version is
http://technet.microsoft.com/zh-cn/library/hh352126(SQL.10).aspx
http://msdn.microsoft.com/zh-cn/library/hh352126(v=SQL.10).aspx
Below is a simple test of mine
All basic functions are available
Including connecting to the database, executing SQL, getting the number and content of records, and getting the number and content of fields
$serverName = "(local)"; //Database server address
$uid = "sa"; //Database username
$pwd = "Admin777"; //Database password
$db_name="DB_FMDISP"; //Database name
$connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>$db_name);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn == false)
{
echo "Connect error!";
Die( print_r( sqlsrv_errors(), true));
}
//Execute sql statement
$stmt = sqlsrv_query($conn, "SELECT TOP 1000 [uid],[uname],[upwd],[udate],[enable] FROM [DB_FMDISP].[dbo].[tb_user]", array(), array ( "Scrollable" => SQLSRV_CURSOR_KEYSET ));
if($stmt === false )
{
echo "Error in executing query.";
Die( print_r( sqlsrv_errors(), true));
}
//Get the number of records
$row_count = sqlsrv_num_rows($stmt);
if ($row_count === false){
echo "nget row errorn".'
';
print_r(sqlsrv_errors(), true);
}
else if ($row_count >=0){
echo 'Number of rows:'."n$row_countn".'
';
}
//Get the number of columns
$field_count = sqlsrv_num_fields($stmt);
if ($field_count === false){
echo "nget row errorn".'
';
print_r(sqlsrv_errors(), true);
}
else if ($field_count >=0){
echo 'Number of columns:'."n$field_countn".'
';
}
//Get the record content of a certain column (by number)
/* Make the first row of the result set available for reading. */
if( sqlsrv_fetch( $stmt ) === false )
{
echo "Error in retrieving row.n";
Die( print_r( sqlsrv_errors(), true));
}
$name = sqlsrv_get_field( $stmt, 0);
echo 'Content:'."$name:".'
';
//show the data
//Assign the record to the array and display it (by column name)
while($row = sqlsrv_fetch_array($stmt)){
echo $row['uid']."-----".$row['uname']."-----".$row['upwd']."
";
} www.2cto.com
/* Free statement and connection resources. */
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
$stmt = sqlsrv_query($conn, "INSERT INTO DB_FMDISP.dbo.tb_user(uname,upwd,udate,enable) VALUES('name','pwd','2010-10-10 10:10:00',1) ");
$stmt = sqlsrv_query($conn, "SELECT * FROM DB_FMDISP.dbo.tb_user");
API Reference (SQL Server Driver for PHP)
The API name for SQL Server Driver for PHP is sqlsrv. All sqlsrv functions begin with sqlsrv_, followed by a verb or noun. Functions followed by a verb are used to perform a specific operation, while functions followed by a noun are used to return a specific form of metadata.
SQL Server Driver for PHP contains the following functions:
Function | Description |
sqlsrv_begin_transaction | Start business. |
sqlsrv_cancel | Cancels a statement; discards any pending results of the corresponding statement. |
sqlsrv_client_info | Provides information about the client. |
sqlsrv_close | Close the connection. Releases all resources associated with the corresponding connection. |
sqlsrv_commit | Commit the transaction. |
sqlsrv_configure | Change error handling and logging configuration. |
sqlsrv_connect | Create a connection and open it. |
sqlsrv_errors | Returns error and/or warning information about the previous operation. |
sqlsrv_execute | Execute predefined statements. |
sqlsrv_fetch | Makes the next row of data available for reading. |
sqlsrv_fetch_array | Retrieve the next row of data as a numeric index array, associative array, or both. |
sqlsrv_fetch_object | Retrieve the next row of data as an object. |
sqlsrv_field_metadata | Returns field metadata. |
sqlsrv_free_stmt | Close statement. Releases all resources associated with the corresponding statement. |
sqlsrv_get_config | Returns the value of the specified configuration setting. |
sqlsrv_get_field | Retrieve a field in the current row by index. PHP return type can be specified. |
sqlsrv_has_rows | Detects whether the result set has one or more rows. |
sqlsrv_next_result | Makes the next result available for processing. |
sqlsrv_num_rows | Reports the number of rows in the result set. |
sqlsrv_num_fields | Retrieve the number of fields in the active result set. |
sqlsrv_prepare | Prepares a Transact-SQL query but does not execute the query. Implicitly bound parameters. |
sqlsrv_query | Prepare Transact-SQL queries and execute them. |
sqlsrv_rollback | Roll back the transaction. |
sqlsrv_rows_affected | Returns the number of modified rows. |
sqlsrv_send_stream_data | Sends up to eight kilobytes (8 KB) of data to the server each time the function is called. |
sqlsrv_server_info | Provides information about the server. |
Excerpted from: The power of focus