php calls the method mssql_fetch_row mssql_fetch_array mssql_fetch_assoc and mssql_fetch_objcect to read the difference

  • 2020-05-19 04:19:55
  • OfStack

Method name: mssql_fetch_row()

Testing:
 
require 'dbconn.php'; 
$sql = 'select * from _Test'; 
$query = mssql_query($sql); 
while($row=mssql_fetch_row($query)) 
{ 
echo $row['UserId'].'::'.$row[1].'<br>'; 
} 

Returns:

Notice: Undefined index: UserId in D:/_PHP_Test/Test2/ test_connLocalDB.php on line 32 :: wang xiao1
Notice: Undefined index: UserId in D:/_PHP_Test/Test2/ test_connLocalDB.php on line 32 :: wang xiao 2
Notice: Undefined index: UserId in D:/_PHP_Test/Test2/ test_connLocalDB.php on line 32 :: wang xiao-3
Notice: Undefined index: UserId in D:/_PHP_Test/Test2/ test_connLocalDB.php on line 32 :: wang xiaowei
Notice: Undefined index: UserId in D:/_PHP_Test/Test2/ test_connLocalDB.php on line 32 :: wang xiaowei

Analysis:

mssql_fetch_row() is exactly the same as mssql_fetch_array() plus the second optional parameter MYSQL_NUM. Gets 1 row of data from the result set associated with the specified result id and returns it as an array. Each result column is stored in a cell of an array, with an offset starting at 0. Note that the offset is from 0. You cannot use the key value (field name), only the index. Therefore, $row['key value '] cannot be used here.

Method name: mssql_fetch_assoc()

Testing:
 
$query = mssql_query($sql); 
while($row=mssql_fetch_assoc($query)) 
{ 
echo $row['UserId'].'::'.$row[1].'<br>'; 
} 

Returns:

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 exactly the same as mssql_fetch_array() plus the second optional parameter MYSQL_ASSOC. It just returns an associative array. This is how mssql_fetch_array() initially works. Therefore, $row[index value] cannot fetch a value.

Method name: mssql_fetch_array()

Testing:
 
<?php 
$query = mssql_query($sql); 
while($row=mssql_fetch_array($query)) 
{ 
echo $row['UserId'].'::'.$row[1].'<br>'; 
} 
?> 


Returns:
1: : king 1
2: : king 2
3: : wang 3
4: : wang 4
5: : wang 5

Analysis:
mssql_fetch_array() is an extended version of mssql_fetch_row(). In addition to storing the data in an array as a numeric index, you can also store the data as an associative index, using the field name as the key name. Therefore, both $row['key value '] and $row[index value] are available.

The second parameter result_type (which is a constant) in mssql_fetch_array() is optional, with values ranging from: MYSQL_ASSOC, MYSQL_NUM and MYSQL_BOTH. Among them:
mssql_fetch_array($query, MYSQL_ASSOC) == mssql_fetch_assoc($query);
mssql_fetch_array($query, MYSQL_NUM) == mssql_fetch_row($query);
So the mssql_fetch_array() function is to some extent a collection of mssql_fetch_row() and mssql_fetch_assoc (). So, mssql_fetch_array() has the MYSQL_BOTH parameter, which gives you an array that contains both a correlation and a numeric index.

Method name: mssql_fetch_object()

Testing:
 
$query=mssql_query($sql); 
while($row=mssql_fetch_object($query)) 
{ 
echo $row->UserId.'::'.$row->CreateTime."<br>"; 
} 

Returns:
1::06 7 2011 4:46PM
2::06 7 2011 4:46PM
3::06 7 2011 4:46PM
4::06 7 2011 4:46PM
5::06 7 2011 4:46PM

Analysis:

mysql_fetch_object() is similar to mssql_fetch_array () except that it returns an object instead of an array and USES the field name as a property. Indirectly, it also means that you can only access the field name, not the offset.

Related articles: