David Cameron has been pitched into fresh controversy over the Coulson phone hacking crisis after the Old Bailey judge accused him of bringing the entire trial close to collapse and demanded an explanation.

As Labour leader Ed Miliband was failing to nail the prime minister over the affair in the Commons, the judge announced that either "in ignorance or deliberately" Cameron's rush to apologise for appointing Coulson had threatened the continuing proceedings on two outstanding charges.

He revealed he had been asked by Coulson's legal team to abandon the trial on Tuesday because of Cameron's "ill-advised and premature" remarks which had declared "open season" on Coulson. He has written to Cameron asking for an explanation.

In the end, the trial was halted on Wednesday when the jurors could not reach agreement on the charges that Coulson and former News of the World royal editor Clive Goodman had paid police officers for royal phone directories.

A decision on whether a re-trial should go ahead is expected to be taken next Monday, but there must now be a question over whether it will happen.

The prime minister's spokesman insisted Cameron had taken "the best possible legal advice" before making his apology, adding: "The prime minister made his statement in response to a verdict in open court. It was restricted to that issue and in response to widespread media reporting."

Labour spokesmen said the issue was "a matter between David Cameron and the judge".

But the affair now raises the question of whether, bearing in mind the judge's comments, a re-trial will be attempted. Coulson's lawyers are likely to argue the media storm prompted by Cameron's remarks might make a fair trial impossible.

And it has raised serious questions over the Cameron team and those who gave him advice that it was acceptable to make his apology, even though the judge had already warned individuals to be careful over what they said while other charges were outstanding.

This is not the first time the prime minister has fallen foul of court proceedings. Earlier this year he was criticised by another judge for expressing support for Nigella Lawson during a fraud trial in which she was a witness.

Meanwhile, during question time in the Commons, Ed Miliband attempted to pin Cameron down on why he failed to heed repeated warnings over the appointment of Coulson.

The prime minister hit back, pointing to the Leveson report which, he said, had found he did nothing wrong.

While Miliband failed to nail the prime minister, he did light the fuse on a small bomb which could yet explode.

He demanded to know whether senior civil servants had also warned the prime minister against appointing Coulson. But he did not get a reply.

Cameron will have left the session believing he may have escaped the worst over the affair, but the judges comments and that lingering question over possible civil service warnings are likely to dog him.

Elsewhere, questions have been raised over the fact this hugely expensive, time consuming affair including the Leveson inquiry and the trials, has ended with only a single successful prosecution.