4

How to copy sql result from one variable to other without arrays and foreach in PHP.

$query="Select * from table";
$res1=mysql_query($query);
$res2=$res1; // I need somethink like this, simply, like in other languages
while($row=mysql_fetch_array($res1)) {
echo $row['Id'];
}
echo '<br>';
while($row=mysql_fetch_array($res2)) {
echo $row['Id'];
}

First while will print id, second not print.

Michael
  • 1,089
  • 1
  • 11
  • 28

2 Answers2

7

For SELECT queries mysql_query() returns a resource on success, which is a special variable holding a reference to the result set. In this statement $res2=$res1; you're only copying the reference to another variable, not the actual result set. And in the first while loop you have already iterated the result set, hence assigning the resource to another variable won't reset it back.

But you can use mysql_data_seek() function to adjust the result pointer to point to the beginning of the result set so that you could iterate through it again.

Here's the reference:

So your code should be like this:

$query="Select * from table";
$res=mysql_query($query);

while($row=mysql_fetch_array($res)) {
    echo $row['Id'];
}
echo '<br />';

mysql_data_seek($res, 0);

while($row=mysql_fetch_array($res)) {
    echo $row['Id'];
}

Sidenote: Don't use mysql_* functions, they are deprecated as of PHP 5.5 and are removed altogether in PHP 7.0. Use mysqli or pdo instead. And this is why you shouldn't use mysql_* functions.

Community
  • 1
  • 1
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • I am amazed. Learned new thing. +10 FROM MY SIDE – Alive to die - Anant Jan 31 '16 at 18:01
  • @A-2-A I am happy that you are amazing on my question. – Michael Jan 31 '16 at 18:05
  • @Rajdeep Paul I will try it, thank you +10 and may be +15 if will work – Michael Jan 31 '16 at 18:06
  • I am amazed with the answer. it tell me new thing. But yes your question is really good. +10 to you also i gave. Please mark this answer, i insist – Alive to die - Anant Jan 31 '16 at 18:07
  • `Please mark this answer` how? I copy variable to variable in other languages and will wery dumped when I can't do this in `PHP` in this case. – Michael Jan 31 '16 at 18:11
  • Why all say don't use `mysql_ is deprecate` . I know and I easy can ad link to db and add an `i` and use mysqli_query($link,''). But this thingh that all say don't, don't every where is terible, is ... – Michael Jan 31 '16 at 18:16
  • @Mitch Actually it's written to let the future visitors know that the `mysql_` extensions are no longer supported. But sometimes it's also helpful for OP, if ofcourse he/she doesn't know it already. – Rajdeep Paul Jan 31 '16 at 18:21
  • _For SELECT queries mysql_query() returns a resource on success, and you can't copy the resource to another variable like this, $res2=$res1;._ Why can't I copy one variable (here resource) to another? – Sohel Ahmed Mesaniya Feb 02 '16 at 17:47
  • 1
    @SohelAhmedM Perhaps my answer needs a little more refinement. I've updated my answer. – Rajdeep Paul Feb 03 '16 at 04:42
2

you can check on this link

$sql = "SELECT * from table";
$result = mysql_query($sql);
$temp_result = $result;
while ($row = mysql_fetch_assoc($temp_result)) {
// do stuff with $row
}
mysql_data_seek($result, 0);
while($row = mysql_fetch_assoc($temp_result)){
// do other stuff with $row
}
Ankit Gupta
  • 182
  • 9