How to delete a row in a query using PDO

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
javi_08
Forum Newbie
Posts: 3
Joined: Tue Dec 28, 2010 1:54 pm

How to delete a row in a query using PDO

Post 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
anantha
Forum Commoner
Posts: 59
Joined: Thu Dec 23, 2010 7:38 pm

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

Post 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..
javi_08
Forum Newbie
Posts: 3
Joined: Tue Dec 28, 2010 1:54 pm

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

Post 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
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

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

Post by Technical »

Use for statement with unset() function.
javi_08
Forum Newbie
Posts: 3
Joined: Tue Dec 28, 2010 1:54 pm

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

Post 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
Post Reply