Php+sqlserver How to operate the SQL Server database

Source: Internet
Author: User
Tags stmt

585,988,722,016 year December 19 17:15:39Hits: 6790In front of the basic configuration of SQL Server environment, now use to learn how to operate the database!! In fact, the method is relatively simple, you can refer to MySQL, as far as possible to compare with MySQL

1, the method of learning, it is best to read the manual
in the previous article it was related to the connection database Sqlsrv_connect (); Remember when we MySQL connected to the database is also mysql_connect (); The operation database is very similar. Starting from the Zero Foundation, you can only refer to the manual. Check out what functions sqlsrv are available for us to use! Recommended URL is: http://php.net/manual/zh/index.php


"Note" A lot of methods are very similar to the method provided by MySQL, see the function name in the following will probably know the usage inside, such as MySQL to execute SQL statements when the call mysql_query (); SQL SQL statements are also called sqlsrv_query (), but it is important to note that they have different parameters for passing. You can only read the manual in detail. Let me briefly summarize how to manipulate the data

2. Connect Database Sqlsrv_connect ()

<?php$serverName ="Servername\sqlexpress";Server's name, local localhost $connectionInfo = array ( " Database "=>" dbName "" UID "=  "UserName",  "PWD" => "password") ;  $conn = Sqlsrv_connect ( $serverName,  $connectionInfo); if ( $conn) {echo " Connection established.<br/> ";} else{echo  "Connection could not being" Established.<br/> "; die (Print_r (sqlsrv_errors (), true));}     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

3. Operation Database
1) Executes the SQL statement sqlsrv_query (), the return value is true or FALSE, where the use of the function is not the same as mysql_query (). It needs to connect the resource handle as a parameter to pass in, see the source code. The resource handle is the "$conn" of the above code to connect to the database.

$sql = "select * from test1";   //sql语句$data = sqlsrv_query($conn,$sql); //$conn资源句柄if($data == true){ die("执行成功");}else{ die("执行失败"); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

2) Get result set

//以数值索引数组、关联数组或这两种数组的形式检索下一行的数据。类似于mysql_fetch_arraysqlsrv_fetch_array  //以对象形式检索下一行的数据。sqlsrv_fetch_object 
    • 1
    • 2
    • 3
    • 4
 $sql =  "select * from Test1";  $conn,  $sql);  $data = = true) {while ( $row = Sqlsrv_fetch_array ( $data, Sqlsrv_fetch_assoc)) {echo  $row [ ' id ']. " , ".  $row [ ' name ']. " <br/> "; }else{die (Print_r (sqlsrv_errors (), true)); }} 
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
 $sql = Span class= "hljs-string" > "select FName, lName from Table_1";  $stmt = sqlsrv_query ( $conn, $ SQL); if ( $stmt = = = false) {die (Print_r (sqlsrv_errors (), true));} while ( $obj = Sqlsrv_fetch_object (  $stmt)) {echo  $obj->fname. ".  $obj->lname.  "<br/>";}            
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

3) Display error message sqlsrv_errors (): Above is useful to this function, as long as the operation of the database error, you can use this function to print out to see the pressure
There's no code here.

I'll make it up when I'm free, not all, no time.

Php+sqlserver How to operate the SQL Server database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.