A Danville man who apparently mistook a cop car for a taxi cab has pleaded no contest to a drug charge.

On Oct. 17, Ongley Raymond Ocon III, 19, of Danville went to a party in San Carlos and needed a ride home, said Steve Wagstaffe, San Mateo County’s chief deputy district attorney.

So he went looking for what he apparently believed was a cab and ended up pressing his face against the window of a Redwood City police car with Officer Carmine Galotta Jr. sitting inside.

Galotta asked Ocon if he needed anything, and Ocon responded “that he needed a ride,” the prosecutor said. Galotta asked Ocon if he had anything illegal on him, and he said, “Yes,” but then paused and said, “No,” Wagstaffe said.

The officer got out of his car and asked Ocon for permission to search him, and Ocon agreed, the prosecutor said. Galotta found a twist of cocaine in Ocon’s pocket and arrested him on suspicion of possession of a controlled substance.

On Wednesday, Superior Court Judge Lisa Novak accepted Ocon’s no-contest plea and ordered him to undergo counseling for 18 months and to abstain from drugs.