MSSQL connection in php

Source: Internet
Author: User
Php connection MSSQL problem PHP MS SQL

The development environment of wamp installed on my local machine, because I want to develop a new site, but the data is stored in the remote MSSQL database
I found two methods to connect to MSSQL on the internet. one method is that I cannot connect to mssql_connect. later, I connect to MSSQL using the method below.
2
/**

* Php uses ODBC to connect to an SQL server database instance
*/

$ Server = 'IP Address or server name ';
$ Username = 'database username ';
$ Password = 'database password ';
$ Database = 'database name ';

$ Connstr = "Driver = {SQL Server}; Server = $ server; Database = $ database ";

If (! Odbc_connect ($ connstr, $ username, $ password, SQL _CUR_USE_ODBC )){
Echo "Couldn't connect to SQL Server on $ server ";
} Else {
Echo "Connect successfully!
";
}
?>
Now the data connection is successful.
But how to write SQL query statements
If I write it like this later, an error will be reported.
If (! Odbc_connect ($ connstr, $ username, $ password, SQL _CUR_USE_ODBC ))
{Echo "Couldn't connect to SQL Server on $ server ";
}
Else {
While ($ row = mssql_fetch_array (mssql_query ('select * from t_item ')))
{

Echo $ row [0];

}
}

Warning: mssql_query () [function. mssql-query]: Unable to connect to server: (null) in C: \ wamp \ www \ mssql. php on line 12

Warning: mssql_query () [function. mssql-query]: A link to the server cocould not be established in C: \ wamp \ www \ mssql. php on line 12

Warning: mssql_fetch_array (): supplied argument is not a valid ms SQL-result resource in C: \ wamp \ www \ mssql. php on line 12


Isn't the query syntax of this connection written like this?


Reply to discussion (solution)

Odbc_connect ($ connstr, $ username, $ password, SQL _CUR_USE_ODBC)
You use odbc to connect to the database
Then you should use odbc function groups for operations.

Odbc_connect ($ connstr, $ username, $ password, SQL _CUR_USE_ODBC)
You use odbc to connect to the database
Then you should use odbc function groups for operations.

If I want to echo the table I want to query, how can I write it?

Is the reason why mssql cannot be connected is that mssql extensions in php are not available?

$ Conn = odbc_connect ($ connstr, $ username, $ password, SQL _CUR_USE_ODBC );
$ Rs = odbc_exec ($ conn, 'select * from tbl_name ');
$ Ar = odbc_fetch_array ($ rs );
Print_r ($ ar );

Is the reason why mssql cannot be connected is that mssql extensions in php are not available?
The extension of mssql is opened, and the version of the ntwdblib. DLL file is still useless. because the business system of mssql does not run very dare on it, it is only necessary to find another way to give up.

$ Conn = odbc_connect ($ connstr, $ username, $ password, SQL _CUR_USE_ODBC );
$ Rs = odbc_exec ($ conn, 'select * from tbl_name ');
$ Ar = odbc_fetch_array ($ rs );
Print_r ($ ar );
First, thank you, bamboo.
I searched Baidu just now for the data that I wrote on these webpages.

$ Conn = odbc_connect ($ connstr, $ username, $ password, SQL _CUR_USE_ODBC); $ SQL = "select * from t_item where fname like '% Nanchang % '"; $ rs = odbc_exec ($ conn, $ SQL); if (! $ Rs) {exit ("Error in SQL");} echo"
 
 
     "; Echo" 
    "; Echo" 
    "; While (odbc_fetch_row ($ rs) {$ fname = odbc_result_all ($ rs, fnumber); $ fnumber = odbc_result ($ rs, fname); echo" 
   "; Echo" 
    ";} Odbc_close ($ conn); echo" 
  
FnumberFname
$ Fname$ Fnumber
";

Can I change the SQL query to the stored procedure in SQL sqrver? how can I call the stored procedure and how can I provide parameters,
Just give me the approximate method.

Isn't that the same? How do you execute it on the console and write it in the program?

The stored procedure is already written. the stored procedure is complicated. many tables have been connected and there are temporary tables. I would like to ask what functions can be used to call the stored procedure in PHP, I have never used the odbc_connect connection method, and I don't know how to continue.

Use pdo, n to see the specific manual. now pdo is popular, and no function is needed.

Odbc_exec executes the SQL command and returns the result
Stored procedures are also SQL commands

The stored procedure is already written. the stored procedure is complicated. many tables have been connected and there are temporary tables. I would like to ask what functions can be used to call the stored procedure in PHP, I have never used the odbc_connect connection method, and I don't know how to continue.

Odbc_exec executes the SQL command and returns the result
Stored procedures are also SQL commands


The stored procedure is already written. the stored procedure is complicated. many tables have been connected and there are temporary tables. I would like to ask what functions can be used to call the stored procedure in PHP, I have never used the odbc_connect connection method, and I don't know how to continue.
I am writing this in the query analyzer.
Select fname = 'nanchang 'into # mssql exec mssql drop table # mssql can be queried normally
Write it like this in PHP
$ SQL = "select fname = 'nanchang 'into # mssql exec mssql drop table # mssql ";
$ Rs = odbc_exec ($ conn, $ SQL );
An error is reported.

Warning: odbc_fetch_row () [function. odbc-fetch-row]: No tuples available at this result index in C: \ wamp \ www \ mssql. php on line 20

If you have Chinese characters, check whether the encoding is correct.

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.