简体   繁体   English

有没有办法使用php取得mysql查询结果

[英]Is there a way using php to take a mysql query result

Is there a way using php to take a MYSQL query result, get a value from a column in the first record fetched, and then put that record back into the query result so when when looping over the result set later, that record can be used again? 有没有一种方法可以使用php来获取MYSQL查询结果,从获取的第一条记录中的列中获取一个值,然后将该记录放回查询结果中,因此当以后遍历结果集时,可以使用该记录再次? If this is possible, can I put the record back in the first position again? 如果可以的话,我可以再次将记录放回第一位置吗?

Or should I return the value that I need from that first row as a separate variable in my routine in MYSQL. 还是应该将第一行中需要的值作为单独的变量返回到MYSQL的例程中。 If this would be the better route to take, can someone give me some insight on how to return both a query result set and a separate variable as well? 如果这是更好的选择,有人可以给我一些有关如何同时返回查询结果集和单独变量的见解吗? I cannot seem to get this to work either. 我似乎也无法使它正常工作。

Or would the best way to do this be to create my own array from the query result set and then manipulate the mysql result set as need? 还是最好的方法是从查询结果集中创建自己的数组,然后根据需要操纵mysql结果集? I'm trying to stay away from this just to cut out the step of creating that array if one of the above two options is possible, otherwise I will just go with this. 如果上述两个选项之一可行,我将尽量避免这样做,只是为了简化创建该数组的步骤,否则,我将继续这样做。

Thanks in advance. 提前致谢。

Just before you need to loop through again, you could use mysql_data_seek($query, 0); 在需要再次循环之前,可以使用mysql_data_seek($query, 0); Then the next call to fetch will be the first row again. 然后,下一次调用fetch将再次成为第一行。

I haven't personally used it, but that's what I understood from the php manual: http://us.php.net/manual/en/function.mysql-data-seek.php 我还没有亲自使用过,但这是我从php手册中了解到的: http : //us.php.net/manual/en/function.mysql-data-seek.php

I agree with the answers above but just to state it a slightly different way: 我同意上面的答案,但只是说了一点不同:

There is no point rewinding a pointer inside the query result object. 在查询结果对象内倒退指针毫无意义。 You have to keep track of where it is, and it's easy to mess up, and it isn't worth the tiny speed increase. 您必须跟踪它的位置,而且很容易弄乱,而且不值得将速度微小提高。

It's much better to make a copy of the entire array and access the records using keys. 最好制作整个数组的副本并使用键访问记录。 Much easier to keep track of what is going on. 跟踪发生的事情要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM