duplicating php resources

夙愿已清 提交于 2019-12-11 16:06:48

问题


are there any way to duplicate the resources produced by functions such as mysql_query?

it may sound dumb but i wanted to reuse such resources so that i won't retype the mysql_query() again and again just to use have a resource..like for example

$rsrc = mysql_query('SELECT * FROM `table` LIMIT 0,1');
$rows = mysql_fetch_array($rsrc);
print_r($rows);

when you fetch a resource, it's value will disappear (that's what i think) what i wanted to do is to reUse it again.i even tried..

$rsrc = mysql_query('SELECT * FROM `table` LIMIT 0,1');
$rsrc_copy = $rsrc;

回答1:


when you fetch a resource, it's value will disappear (that's what i think) what i wanted to do is to reUse it again.i even tried..

What you are experiencing is not the value disappearing, but the result's internal data pointer moving one row ahead.

It is theoretically possible to move the internal data pointer around using mysql_data_seek(), but it is rarely practical to do so. I've never seen this used in a real world application. Rather re-use the result you fetched, e.g. by storing the rows in an array.




回答2:


You don't need to reuse the resource. You can reuse the rows.




回答3:


Put the result in an array and use it as many times as you like.

$results = array();
$qry = mysql_query("SELECT * from ...");
while($row = mysql_fetch_array($qry)){
  $results[] = $row;
}

// use the $results array


来源:https://stackoverflow.com/questions/3948420/duplicating-php-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!