Does PHP have a construct similar to .NET's Da

2019-02-25 10:48发布

How can I implement a DataSet in PHP like .NET?

I want this class to read data from database only once, then I should be able to use the data without connecting again to MySQL to run queries.

select * from user

When I run this query on the DataSet the data is fetched from memory.

How can I implement this mechanism in PHP?

3条回答
【Aperson】
2楼-- · 2019-02-25 11:19

I don't think php can provide what you're looking for as it's very much text code and HTML driven, so you don't get the fancy GUI objects like you do in .net. PEAR provides a lot of source code that will produce data grids, that you could possibly investigate, all stored in code like arrays etc. I'm relatively new to php, so maybe someone would disagree...

查看更多
Root(大扎)
3楼-- · 2019-02-25 11:21

What you are describing is the DataSet / DataTable data container classes of .NET which can work in disconnected mode.

A DataReader is kind of a cursor and needs to have the connection open in order to move through the result set of the underlying query.

In PHP you could do this:

<?PHP

$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

  $SQL = "SELECT * FROM tb_address_book";
  $result = mysql_query($SQL);

  while ($db_field = mysql_fetch_assoc($result)) {
    print $db_field['ID'] . "<BR>";
    print $db_field['First_Name'] . "<BR>";
    print $db_field['Surname'] . "<BR>";
    print $db_field['Address'] . "<BR>";
  }

  mysql_close($db_handle);

}
else {
  print "Database NOT Found ";
  mysql_close($db_handle);
}

?>
查看更多
时光不老,我们不散
4楼-- · 2019-02-25 11:26

You could push your data into an array like this:

$result = mysql_query( 'select * from user' );

$results = array();
while ( $row = mysql_fetch_array( $result ) ) {
    array_push( $results, $row );
}

mysql_close();

Then you can do whatever operations you want on the array...

foreach( $results as $record ){
    $foo = $record['col_name'];
    //...
}
查看更多
登录 后发表回答