本文主要是介绍php72连接mssql 2017,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
数据库结构:
表内容:
代码列表:
<?php
$serverName = "192.168.1.196";
$connectionOption = array("Database" => "SampleDB","Uid" => "SA","PWD" => "tjjingpan_2018"
);
$conn = sqlsrv_connect($serverName,$connectionOption);if ($conn)
{echo "Connection established.";echo "</br>";
}else
{echo "Connection could not be established.<br/>";die(print_r(sqlsrv_errors(),true));
}//Select from SampleTable
$tsql = "select * from Sample_Table;";
$getResults = sqlsrv_query($conn,$tsql);
echo ("Reading data from SampleTable.<br/>");
if($getResults)
{while ($row = sqlsrv_fetch_array($getResults,SQLSRV_FETCH_ASSOC)){echo ($row['Number'] ." " .$row['First_Name'] . " " . $row['Last_Name'] .PHP_EOL);echo '<br/>';}
}
else
{echo "select failed.\n";die(print_r(sqlsrv_errors(), true));
}
sqlsrv_free_stmt($getResults);//Update from Sample_Table
$userToUpdate = 'RedHat';
$tsql= "UPDATE Sample_Table SET Last_Name = ?WHERE First_Name = ?";
$params = array('Maipo', $userToUpdate);
echo("\nUpdating Last_Name for user " . $userToUpdate . PHP_EOL);$getResults = sqlsrv_query($conn,$tsql,$params);
if($getResults)
{echo "鏇存柊鎴愬姛!<br/>";
}
else
{echo "鏇存柊澶辫触!<br/>";}$rowsAffected = sqlsrv_rows_affected($getResults);
if ($getResults == FALSE or $rowsAffected == FALSE)die(print_r(sqlsrv_errors(),true));
echo ($rowsAffected. " row(s) updated: " . "<br/>");
sqlsrv_free_stmt($getResults);//Select from SampleTable
$tsql = "select * from Sample_Table;";
$getResults = sqlsrv_query($conn,$tsql);
echo ("Reading data from SampleTable.<br/>");
if($getResults)
{while ($row = sqlsrv_fetch_array($getResults,SQLSRV_FETCH_ASSOC)){echo ($row['Number'] ." " .$row['First_Name'] . " " . $row['Last_Name'] .PHP_EOL);echo '<br/>';}
}
else
{echo "select failed.\n";die(print_r(sqlsrv_errors(), true));
}
sqlsrv_free_stmt($getResults);//Insert from Sample_table
$tsql = "INSERT INTO Sample_Table (Number,First_Name,Last_Name,Last_Update) values (?,?,?,?);";
$params = array('00005','Debian','Linux','2018-01-01');
$getResults = sqlsrv_query($conn,$tsql,$params);
echo ("Insering a new row into Sample_Table.");
$rowAffected = sqlsrv_rows_affected($getResults);
if($getResults == FALSE or $rowAffected == FALSE)die(print_r(sqlsrv_errors(), true));
echo ($rowsAffected. "row(s) inserted."."<br/>" );
sqlsrv_free_stmt($getResults);// Delete from Sample_Table
$userToDelete = 'Debian';
$tsql = "DELETE FROM Sample_Table WHERE First_name = ?";
$params = array($userToDelete);
$getResults = sqlsrv_query($conn,$tsql,$params);
echo("鍒犻櫎鐢ㄦ埛".$userToDelete)."銆?;
$rowAffected = sqlsrv_rows_affected($getResults);
if($getResults == FALSE or $rowAffected == FALSE)die(print_r(sqlsrv_errors(), true));
echo ($rowAffected. "row(s) deleted. <br\>" );
sqlsrv_free_stmt($getResults);
sqlsrv_close($conn);?>
这篇关于php72连接mssql 2017的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!