duplicating php resources

2019-09-04 08:29发布

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;

3条回答
地球回转人心会变
2楼-- · 2019-09-04 09:14

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.

查看更多
来,给爷笑一个
3楼-- · 2019-09-04 09:18

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

查看更多
我只想做你的唯一
4楼-- · 2019-09-04 09:31

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
查看更多
登录 后发表回答