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
让我们假设我们有一个数据库,并且我们已经建立了与数据库的连接。我们假设有一个名为table的表。表内有两列id和name。表中有5行数据。表结构是
| 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.
似乎当我对mysql_query的结果执行一个函数时,该函数只是从结果中删除第一行,即使它在调用函数之前存储在其他变量中也是如此。
If i add a condition WHERE id
= 1 then the second function returns false, like:
如果我添加一个条件WHERE id = 1,那么第二个函数返回false,如:
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.
为什么会这样?提前致谢。
1 个解决方案
#1
2
This line doesn't return the "result", it returns a resource object that you can use to access the result:
该行不返回“结果”,它返回一个可用于访问结果的资源对象:
$result = mysql_query("SELECT * FROM `table`");
These lines just assign the same resource to two different variables:
这些行只是将相同的资源分配给两个不同的变量:
$result1 = $result;
$result2 = $result;
You shouldn't think of it as two separate result sets. You've just given two different names for the same resource.
您不应将其视为两个单独的结果集。您刚刚为同一资源指定了两个不同的名称。
#1
2
This line doesn't return the "result", it returns a resource object that you can use to access the result:
该行不返回“结果”,它返回一个可用于访问结果的资源对象:
$result = mysql_query("SELECT * FROM `table`");
These lines just assign the same resource to two different variables:
这些行只是将相同的资源分配给两个不同的变量:
$result1 = $result;
$result2 = $result;
You shouldn't think of it as two separate result sets. You've just given two different names for the same resource.
您不应将其视为两个单独的结果集。您刚刚为同一资源指定了两个不同的名称。