简体   繁体   English

将mysql查询放入数组并使用多个错误

[英]putting mysql query into array and using more than once error

I have a real big problem that normally doesn't happen. 我有一个通常不会发生的真正大问题。 For some reason on this occasion it errors... 由于某种原因,它会出错...

Ok I put in a mysql query IE 好吧,我把一个MySQL查询IE

$query = mysql_query("SELECT * from tablename");

Then i say 那我说

$row = mysql_fetch_array($query,MYSQL_BOTH)

When I put (even a blank loop in it gives me an error) a loop in... IE 当我放(甚至是一个空白循环会给我一个错误)在... IE中循环

foreach ($row as $rowme){
 // do nothing
   echo $rowme['id'] . "<br />";
}

I get invalid argument supplied for foreach() 我得到了为foreach()提供的无效参数

Never before has this happened - any ideas why it is now? 从来没有发生过这种情况-为什么现在如此?

Running PHP 5.2.11 on the server 在服务器上运行PHP 5.2.11

while($row = mysql_fetch_assoc) = works fine, but I can use this only once on the page... another question why can't I use the same loop again somewhere else? while($row = mysql_fetch_assoc) =可以正常工作,但是我只能在页面上使用一次……另一个问题,为什么我不能在其他地方再次使用相同的循环? (Coldfusion you can use the same loop from a query repeatedly? why does not PHP do this using mysql_fetch_assoc ???) (Coldfusion您可以重复使用查询中的同一循环?PHP为什么不使用mysql_fetch_assoc做到这一点?)

I'm using a jQuery photo gallery, which populates some repeated javascript to write the descriptions on the images via the class and repeats the list items they are in, hence I need two loops of the same query. 我正在使用jQuery照相馆,该照相馆会填充一些重复的JavaScript,以通过类在图像上写描述,并重复它们所在的列表项,因此我需要同一查询的两个循环。 So obviously putting the query results into an array and using the array repeatedly should work right? 因此,显然将查询结果放入数组中并重复使用该数组应该正确吗?

The only other option is to create two queries exactly the same. 唯一的选择是创建两个完全相同的查询。 Seems inefficient however. 但是似乎效率低下。

Can anyone answer these questions or shed some light. 任何人都可以回答这些问题或阐明一些看法。 I'm desperate and very very behind putting in this gallery, (why I'm still here at work at 1.22 am) - Would be most grateful. 我很拼命,非常不喜欢在这个画廊里放东西((为什么我在1.22 am还在这里工作)-非常感谢。

Thanks 谢谢

A. 一种。

while($row = mysql_fetch_assoc) = works fine, but I can use this only once on the page... while($row = mysql_fetch_assoc) =可以正常工作,但是我只能在页面上使用一次...

Not so; 不是这样; you could write if ($row = mysql_fetch_assoc(...)) as many times as you like. 您可以根据需要多次编写if ($row = mysql_fetch_assoc(...))

However in your case, you probably want to use the "seek" functions that let you go back to the start of the resultset if you want to iterate it again. 但是,对于您的情况,您可能想使用“搜索”功能 ,如果您要再次迭代结果集,则可以返回到结果集的开头。

I get invalid argument supplied for foreach() 我得到了为foreach()提供的无效参数

You never checked that $row is an array. 您从未检查过$row是一个数组。 If there are no results, $row will be FALSE . 如果没有结果,则$row将为FALSE

There are two options to your "dilemma". 您的“困境”有两种选择。 You cannot query the results twice in PHP without telling explicitly. 您不能在PHP中两次查询结果而无需明确告知。

But a simple option is to store them away into an array: 但是一个简单的选择是将它们存储到数组中:

$query = mysql_query("SELECT * from tablename");
while ($row = mysql_fetch_array($query,MYSQL_BOTH)) {
    $saved[] = $row;
}

This allows you to use the result set twice, and also makes your example foreach construct work: 这使您可以两次使用结果集,并使每个构造示例都可以工作:

foreach ($saved as $rowme){
     echo $rowme['id'] . "<br />";

Obviously only advisable if it's few result rows. 显然,仅在结果行很少的情况下才是明智的。


The second option is to "rewind" the results. 第二种选择是“倒带”结果。 So you can actually query with _fetch_assoc twice: 因此,您实际上可以使用_fetch_assoc查询两次:

$query = mysql_query("SELECT * from tablename");

// first time
while ($row = mysql_fetch_array($query,MYSQL_BOTH)) {
    ...
}


// other code here
...


// second iteration over results
mysql_data_seek($query, 0);   // rewind
while ($row = mysql_fetch_array($query,MYSQL_BOTH)) {
    ...
}

See http://php.net/mysql_data_seek in the manual. 参见手册中的http://php.net/mysql_data_seek

If you want an array there it the way that you build the array with your stream. 如果您想要一个数组,可以使用流构建数组的方式。

$data = array();

while($row = mysql_fetch_assoc($res)) {
    $data[] = $row;
}

But when you do this you need a lot of memory and when your array is to big and you come over the the momory_limit which you can define in the php.ini you get an error like 但是,当您执行此操作时,您将需要大量的内存,并且当您的数组很大时,您遇到了可以在php.ini中定义的momory_limit,您将收到类似

"Fatal error: Allowed memory size of xxxxxxx bytes exh..." “致命错误:允许的xxxxxxx字节内存大小为exh ...”

I think you slightly misunderstand what each mysql function does. 我认为您稍微误解了每个mysql函数的作用。 both the names you've chosen and the way you try to use them show this. 您选择的名称和尝试使用它们的方式都会显示出来。

I'll try to show an example with descriptive variable names. 我将尝试显示一个具有描述性变量名称的示例。

$query = "SELECT * FROM tablename";   //$query is sql query
$res = mysql_query($quwey);           //$res is resource

A resource encapsulates the results of the query. 资源封装了查询结果。 You use mysql_fetch functions to get data out. 您可以使用mysql_fetch函数获取数据。 I'll use mysql_fetch_assoc to get one row at a time but mysql_fetch_array , mysql_fetch_object are fine too. 我将使用mysql_fetch_assoc获取一行,但是mysql_fetch_arraymysql_fetch_object也可以。 Every time it gets called you get one row. 每次调用它时,您都会得到一行。 while loops are a good choice for this bc they will fetching and re-assigning the new rows until there are none left. while循环对于此BC是一个不错的选择,但它们将获取并重新分配新的行,直到没有剩余的行为止。

while($row = mysql_fetch_assoc($res)) //$row is an associative array for single row.  
                                      //one element for every field of table
{
   var_dump($row); //this will show you what $row looks like
}

You can iterate on $row with foreach , it will step thru every element of $row . 您可以遍历$rowforeach ,它会加强通的每一个元素$row That is, every field for a single row of the table. 即,表的单个行的每个字段。 This will NOT visit more than one row. 这将不会访问超过一行。

while($row = mysql_fetch_assoc($res)) 
{
   //$row['id'] makes sense
   foreach($row as $fieldName=>$fieldValue)
   {
       echo "$fieldName -> $fieldValue <br />;
       //$fieldName['id'] and $fieldValue['id'] do not make sense. they are not arrays
   }
}

You were concerned that you can only use that while/fetch thing once, but thats not the case. 您担心只能使用一次while / fetch事情,但事实并非如此。 You can reset the resource's pointer back to the top and do the while loop all over again: 您可以将资源的指针重设回顶部,然后再次执行while循环:

 while($row = mysql_fetch_assoc($res)) {//do stuff}
 mysql_data_seek($res, 0); //reset pointer back to top
 while($row = mysql_fetch_assoc($res)) {//do stuff again}    

Or you can store all of the rows into one (multidimensional) array you can work with later as many times as you like. 或者,您可以将所有行存储到一个(多维)数组中,以后可以任意多次使用。

$arrayOfRows = array();
while($row = mysql_fetch_assoc($res)) //$row is a single row.  one element for every field of table
{
   $arrayOfRows[] = $row;
}

var_dump($arrayOfRows); //see what this looks like

And this is how you can loop thru this $arrayOfRows and use it later. 这就是您可以循环通过$arrayOfRows并在以后使用的方法。

foreach($arrayOfRows as $oneRow)
{
    foreach($oneRow as $fieldName=>$fieldValue)
    {
         //code here
    }
}

I suspect the error is directly linked to the number of results. 我怀疑该错误与结果数直接相关。 From the manual you can see the return type is either an array or FALSE. 手册中可以看到,返回类型是数组或FALSE。 The latter might give you the error you are describing. 后者可能会给您所描述的错误。

Fix it with either mysql_num_rows() or 使用mysql_num_rows()或修复它

if($row){
    foreach ($row as $rowme){
        // do nothing
        echo $rowme['id'] . "<br />";
    }
}

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

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