I tried this as a test purpose. And I don’t know why is this happening. That’s I need help from the experts. Thanks.
Lets assume that we have a database and we have established connection with the database. Let’s assume there is a table named table. Inside the table there are two columns- id and name. there are 5 rows of data in the table. Table structure is
| id | name |
---------------
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
| 4 | name4 |
| 5 | name5 |
Now my code goes here-
<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$result = mysql_query("SELECT * FROM `table`");
/* let's store this value in two different variables. */
$result1 = $result;
$result2 = $result;
/* let's perform mysql_fetch_array() and mysql_fetch_row() functions */
$result22 = mysql_fetch_row($result);
var_dump($result22);
$result11 = mysql_fetch_array($result);
var_dump($result11);
?>
it results :
array
0 => string '1' (length=1)
1 => string 'name1' (length=5)
array
0 => string '2' (length=1)
'id' => string '2' (length=1)
1 => string 'name2' (length=5)
'name' => string 'name2' (length=5)
If i alter the order of the functions it results :
array
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string 'name1' (length=5)
'name' => string 'name1' (length=5)
array
0 => string '2' (length=1)
1 => string 'name2' (length=5)
It seems that when I perform a function upon the result of mysql_query, that function simply removes the first row from the result even if it is stored in other variable before
calling the function.
If i add a condition WHERE id = 1 then the second function returns false, like:
array
0 => string '1' (length=1)
'id' => string '1' (length=1)
1 => string 'name1' (length=5)
'name' => string 'name1' (length=5)
boolean false
Why is this happening? Thanks in advance.
This line doesn’t return the “result”, it returns a resource object that you can use to access the result:
These lines just assign the same resource to two different variables:
You shouldn’t think of it as two separate result sets. You’ve just given two different names for the same resource.