0

This is not working

$restab=mysql_query("SELECT `tb_id`, `tb_head` FROM `tbs` where dashboard_id=$dash_board_id") or die(mysql_error());
$resbody=$restab;     

This is working

$restab=mysql_query("SELECT `tb_id`, `tb_head` FROM `tbs` where dashboard_id=$dash_board_id") or die(mysql_error());
$resbody=mysql_query("SELECT `tb_id`, `tb_head` FROM `tbs` where dashboard_id=$dash_board_id") or die(mysql_error());
Syed Daniyal Asif
  • 726
  • 1
  • 5
  • 19
  • 3
    Why do you actually want to duplicate the query resultset? – Mark Baker Aug 11 '14 at 09:08
  • 1
    i have to use 2 while loops at different places – Syed Daniyal Asif Aug 11 '14 at 09:11
  • 2
    Use mysql_data_seek($restab, 0) to reset the result pointer before the second loop - [see PHP docs](http://www.php.net/manual/en/function.mysql-data-seek.php) – Mark Baker Aug 11 '14 at 09:13
  • But switch to MySQLi rather than use the deprecated MySQL extension, and the equivalent to reset the resultset pointer with MySQLi is `data_seek(0)` – Mark Baker Aug 11 '14 at 09:17
  • possible duplicate of [Can you reuse a mysql result set in PHP?](http://stackoverflow.com/questions/4638014/can-you-reuse-a-mysql-result-set-in-php) – Athafoud Aug 11 '14 at 09:24

4 Answers4

1
$result = mysql_query("SELECT `tb_id`, `tb_head` FROM `tbs` where dashboard_id=$dash_board_id")
    or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {
    .... do some stuff
}

mysql_data_seek($result, 0);
while($row = mysql_fetch_assoc($result)) {
    .... do some more stuff
}

But as the mysql extension is deprecated, you really should be switching to mysqli or pdo, and learning to use bind variables

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

Major update:

Create var:

$query = "SELECT `tb_id`, `tb_head` FROM `tbs` where dashboard_id=$dash_board_id";

Call function multiple times:

$resbody = mysql_query($query) or die(mysql_error());
$restab = mysql_query($query);

Do not use deprecated function nex time, take a look at MYSQLI or PDO

m1k1o
  • 2,344
  • 16
  • 27
0

If you really want to duplicate the result object, I propose to clone the object using php clone function

Something like this

$resbody = clone $restab;
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Athafoud
  • 2,898
  • 3
  • 40
  • 58
0

If you do not want to make the same query again, use
mysql_data_seek($restab,0)
And then use it again. The internal row pointer will be reset.
More info here: http://www.w3schools.com/php/func_mysql_data_seek.asp

ovi
  • 566
  • 4
  • 17