In php, mssql_fetch_row, mssql_fetch_array, mssql_fetch_assoc, and mssql_fetch_objcect read data.

Source: Internet
Author: User
In php, mssql_fetch_row, mssql_fetch_array, mssql_fetch_assoc, and mssql_fetch_objcect read data.

  1. Require 'dbconn. php ';
  2. $ SQL = 'select * from _ test ';
  3. $ Query = mssql_query ($ SQL );
  4. While ($ row = mssql_fetch_row ($ query ))
  5. {
  6. Echo $ row ['userid']. ':'. $ row [1].'
    ';
  7. }

Return: 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 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.

2. example of method name mssql_fetch_assoc:

  1. $ Query = mssql_query ($ SQL );
  2. While ($ row = mssql_fetch_assoc ($ query ))
  3. {
  4. Echo $ row ['userid']. ':'. $ row [1].'
    ';
  5. }

Return: 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_con NLocalDB. php on line 43 5: Analysis: mssql_fetch_assoc () is exactly the same as using 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.

III. example of method name mssql_fetch_array:

  1. $ Query = mssql_query ($ SQL );
  2. While ($ row = mssql_fetch_array ($ query ))
  3. {
  4. Echo $ row ['userid']. ':'. $ row [1].'
    ';
  5. }
  6. ?>

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) = explain ($ query); mssql_fetch_array ($ query, MYSQL_NUM) = mssql_fetch_row ($ query); so mssql_fetch_array () to some extent, a function can be regarded as a collection of mssql_fetch_row () and mssql_fetch_assoc. Therefore, mssql_fetch_array () also has the MYSQL_BOTH parameter, and an array containing both the correlation and numerical indexes is obtained.

IV. example of method name mssql_fetch_object:

  1. $ Query = mssql_query ($ SQL );
  2. While ($ row = mssql_fetch_object ($ query ))
  3. {
  4. Echo $ row-> UserId. ':'. $ row-> CreateTime ."
    ";
  5. }

Return value: 1: 06 7 2011 4: 46PM2: 06 7 2011 4: 46PM3: 06 7 2011 4: 46PM4: 06 7 2011 4: 46PM5 :: 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.