Php call Methods: mssql_fetch_row, mssql_fetch_array, and mssql_fetch_a

Source: Internet
Author: User
Mysql_fetch_object () is similar to mssql_fetch_array. Only one difference is that an object is returned instead of an array and the field name is used as an attribute. Indirectly, it also means that only the field names can be used for access, rather than the offset.

Mysql_fetch_object () is similar to mssql_fetch_array. Only one difference is that an object is returned instead of an array and the field name is used as an attribute. Indirectly, it also means that only the field names can be used for access, rather than the offset.

Method Name: mssql_fetch_row ()

Test:
The Code is as follows:
Require 'dbconn. php ';
$ SQL = 'select * from _ test ';
$ Query = mssql_query ($ SQL );
While ($ row = mssql_fetch_row ($ query ))
{
Echo $ row ['userid']. ':'. $ row [1].'
';
}

Return Value:

Notice: Undefined index: UserId in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 32: Wang Xiaoyi
Notice: Undefined index: UserId in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 32: Wang xiao'er
Notice: Undefined index: UserId in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 32: Wang xiaosan
Notice: Undefined index: UserId in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 32: Wang xiaosi
Notice: Undefined index: UserId in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 32: Wang xiaowu

Analysis:

Mssql_fetch_row (), which is exactly the same as using mssql_fetch_array () and the second optional parameter MYSQL_NUM. Obtain a row of data from the result set associated with the specified result ID and return it as an array. Each result column is stored in an array unit, and the offset starts from 0. Note: here the offset starts from 0. You cannot use the key value (field name) to take the value. You can only use the index to take the value. Therefore, $ row ['key value'] cannot be used to obtain the value.

Method Name: mssql_fetch_assoc ()

Test:
The Code is as follows:
$ Query = mssql_query ($ SQL );
While ($ row = mssql_fetch_assoc ($ query ))
{
Echo $ row ['userid']. ':'. $ row [1].'
';
}

Return Value:

Notice: Undefined offset: 1 in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 43 1 ::
Notice: Undefined offset: 1 in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 43 2 ::
Notice: Undefined offset: 1 in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 43 3 ::
Notice: Undefined offset: 1 in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 43 4 ::
Notice: Undefined offset: 1 in D:/_ PHP_Test/Test2/test_connLocalDB.php on line 43 5 ::
Analysis:
Mssql_fetch_assoc () is identical to mssql_fetch_array () and the second optional parameter MYSQL_ASSOC. It only returns the associated array. This is also the initial working method from mssql_fetch_array. Therefore, $ row [index value] cannot be used to obtain the value.

Method Name: mssql_fetch_array ()

Test:
The Code is as follows:
$ Query = mssql_query ($ SQL );
While ($ row = mssql_fetch_array ($ query ))
{
Echo $ row ['userid']. ':'. $ row [1].'
';
}
?>


Return Value:
1: Wang Xiaoyi
2: Wang xiao'er
3: Wang xiaosan
4: Wang xiaosi
5: Wang xiaowu

Analysis:
Mssql_fetch_array (), which is an extended version of mssql_fetch_row. In addition to storing data in an array as a digital index, you can also store data as an associated index and use the field name as the key name. Therefore, both $ row ['key value'] and $ row [index value] can be used here.

In mssql_fetch_array (), the second parameter result_type (which is a constant) is an optional parameter. The optional values include MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. Where:
Mssql_fetch_array ($ query, MYSQL_ASSOC) = mssql_fetch_assoc ($ query );
Mssql_fetch_array ($ query, MYSQL_NUM) = mssql_fetch_row ($ query );
Therefore, the mssql_fetch_array () function can be regarded as a collection of mssql_fetch_row () and mssql_fetch_assoc () to some extent. Therefore, mssql_fetch_array () also has the MYSQL_BOTH parameter, and an array containing both the correlation and numerical indexes is obtained.

Method Name: mssql_fetch_object ()

Test:
The Code is as follows:
$ Query = mssql_query ($ SQL );
While ($ row = mssql_fetch_object ($ query ))
{
Echo $ row-> UserId. ':'. $ row-> CreateTime ."
";
}

Return Value:
1: 06 7 2011 PM
2: 06 7 2011 PM
3: 06 7 2011 PM
4: 06 7 2011 PM
5: 06 7 2011 PM

Analysis:

Mysql_fetch_object () is similar to mssql_fetch_array. Only one difference is that an object is returned instead of an array and the field name is used as an attribute. Indirectly, it also means that you can only access it by field name, rather than offset.

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.