开发者

mysql_query function in php

开发者 https://www.devze.com 2023-01-22 19:36 出处:网络
<?php $con= mysql_connect(\"localhost\",\"root\",\"mysql\"); mysql_select_db(\"Db_name\",$con); $res=mysql_query(\"select *from table_name\");
<?php 
  $con= mysql_connect("localhost","root","mysql");
  mysql_select_db("Db_name",$con);

  $res=mysql_query("select *from table_name");

  mysql_close($con);   // closing connection before 开发者_StackOverflowfetching contents.

  while($r=mysql_fetch_array($res)) {
    echo $r['ename'];
  }

?>

This programs works even if i close the connection before fetching contents from the table.

In order get table contents from the $res connection is not necessary ?

Is$res just a program variable ? If so what kind of data structure it is using(associative array ? )

In oracle we have implicit cursor and explicit cursor. Are there any equivalent things in mysql?

In the above program where cursors come into picture ?


$res in your case is a special type called a "Resource". Simply put it is a collection of the data returned which the mysql_fetch_* functions operate. As such, it can live beyond the connection. Check the documentation for more details.


The MySQL client library will fetch the whole result set before the call to mysql_query() returns which explains why your code works.

You can use mysql_unbuffered_query() to fetch the rows incrementally in which case you must keep the connection open.

Note that fetching the rows is handled inside the MySQL client code. $res is just an opaque ressource type that represents an internal resultset object (buffered or not). You can only operate on that ressource by passing it to other MySQL functions.


In order get table contents from the $res connection is not necessary ?

Why did not you try it and see?

Anyway, what is the reason to close connection manually?

Is $res just a program varible ? If so what kind of data structure it is using(associative array ?

Yes, it is a variable. You can see what is inside with var_dump($res);

In oracle we have implicit cursor and explicit cursor.Are there any equivalent things in mysql ? In the above program where cursors come into picture ?

What exact task are you trying to solve?


As jason said $res is just like a normal variable of type Resource. This variable holds its value even after the mysql connection is closed as it has no connection with the mysql connection.

0

精彩评论

暂无评论...
验证码 换一张
取 消