php 简单的MS-SQL示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 简单的MS-SQL示例相关的知识,希望对你有一定的参考价值。
<span style="color: #808080; font-style: italic;">/*
** Connect to database:
*/</span>
<span style="color: #808080; font-style: italic;">// Connect to the database (host, username, password)</span>
<span style="color: #0000ff;">$con</span> = mssql_connect<span style="color: #66cc66;">(</span><span style="color: #ff0000;">'localhost'</span>,<span style="color: #ff0000;">'admin'</span>,<span style="color: #ff0000;">'foo'</span><span style="color: #66cc66;">)</span>
or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Could not connect to the server!'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Select a database:</span>
mssql_select_db<span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Northwind'</span><span style="color: #66cc66;">)</span>
or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'Could not select a database.'</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Example query: (TOP 10 equal LIMIT 0,10 in MySQL)</span>
<span style="color: #0000ff;">$SQL</span> = <span style="color: #ff0000;">"SELECT TOP 10 * FROM ExampleTable ORDER BY ID ASC"</span>;
<span style="color: #808080; font-style: italic;">// Execute query:</span>
<span style="color: #0000ff;">$result</span> = mssql_query<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$SQL</span><span style="color: #66cc66;">)</span>
or <a href="http://www.php.net/die"><span style="color: #000066;">die</span></a><span style="color: #66cc66;">(</span><span style="color: #ff0000;">'A error occured: '</span> . <a href="http://www.php.net/mysql_error"><span style="color: #000066;">mysql_error</span></a><span style="color: #66cc66;">(</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span>;
<span style="color: #808080; font-style: italic;">// Get result count:</span>
<span style="color: #0000ff;">$Count</span> = mssql_num_rows<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">)</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">"Showing $count rows:<hr/><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #808080; font-style: italic;">// Fetch rows:</span>
<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">(</span><span style="color: #0000ff;">$Row</span> = mssql_fetch_assoc<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$result</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$Row</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">'Fieldname'</span><span style="color: #66cc66;">]</span> . <span style="color: #ff0000;">"<span style="color: #000099; font-weight: bold;">\n</span>"</span>;
<span style="color: #66cc66;">}</span>
mssql_close<span style="color: #66cc66;">(</span><span style="color: #0000ff;">$con</span><span style="color: #66cc66;">)</span>;
以上是关于php 简单的MS-SQL示例的主要内容,如果未能解决你的问题,请参考以下文章
如何将 MS-SQL Server SELECT 查询转换/迁移到 Oracle 和 MySQL?
MS-SQL 2012 中的 C# Dapper 加载程序在插入时出现死锁