Page 1 of 1

How to delete a row in a query using PDO

Posted: Tue Dec 28, 2010 2:07 pm
by javi_08
Hi everybody

First of all, I'm not a native english speaker so I'm sorry for my writting.

I'm developing a web application and I'm using PDO for the comunication with the database and I have several classes which I use for the results.

The point is I have to do a select in a script, and after, regarding to a external event, delete one of them.

I'm reading the results by this way:

Code: Select all

foreach ($query as $rows_query)
{
    //code...
}
$rows_query is a class with some methods.

How coud I delete one row of $query??

Actually the foreach use a copy of $query so I have no idea about how to delete one row of the original query ($query)

Thank you for your attention and I'm sorry again for my english.

Greetings

Re: How to delete a row in a query using PDO

Posted: Tue Dec 28, 2010 5:06 pm
by anantha
i am not sure about it...are you asking like this
foreach($query as &$rows_query)
{
$rows_query
}

the above will reference instead of copying...for more info go to foreach manual in php..you can find more info there..

Re: How to delete a row in a query using PDO

Posted: Tue Dec 28, 2010 6:16 pm
by javi_08
I
anantha wrote:i am not sure about it...are you asking like this
foreach($query as &$rows_query)
{
$rows_query
}

the above will reference instead of copying...for more info go to foreach manual in php..you can find more info there..
I have tried that, but I get this.... Fatal error: An iterator cannot be used with foreach by reference

I have been reading on php.net and I have no answer.... I thought the below would run, but I get the same error.

Code: Select all

foreach ($queryT as &$rows_query)
{
           //......
            if (cond)
                    $rows_query=NULL;

}
I'm lost :S

thank you anyway for your answer

Re: How to delete a row in a query using PDO

Posted: Tue Dec 28, 2010 11:27 pm
by Technical
Use for statement with unset() function.

Re: How to delete a row in a query using PDO

Posted: Sat Jan 01, 2011 4:14 pm
by javi_08
First of all, thank you for the answers.

Technical, could you show me how to implement that for statement? Do you think about anything like that? :

Code: Select all

for($i=0;$i<count($queryT);$i++){
//code
if(cond)
unset (&$queryT[$i]);
//code
}

Now I have patched temporarily my problem in the view, just before the representation (in the view a table is generated with the rows), I'm checking the condition in every row, showing or not that row. But if I use $query->rowCount(), (for the tfoot) that valor is not valid, also It would be better to do this in the controller.

Greetings