The Giants averted a sweep in a matchup of NL wild-card contenders, stopping their three-game slide and ending the Phillies' four-game winning streak.

"The last thing you want to do is dig yourself too big a hole. I thought this was a big game for the guys," manager Bruce Bochy said. "We came out after a tough series against San Diego at home, then lost the first two here. To get this last one against a very tough pitcher is big for this club."

Sanchez publicly predicted a sweep before the Giants faced the NL West-leadingSan Diego Padres, but the Padres beat the left-hander in the opening game and took two of three.

Sanchez (9-8) redeemed himself against the Phillies, becoming the first Giants starting pitcher to record a win in 15 games, dating to his victory over the Colorado Rockies on Aug. 3. No runner reached second base against the lefty, who retired 15 straight after hitting Chase Utley with a pitch in the fourth.

"He had a great look about him," Bochy said. "The last couple of starts, especially the comments, he'd probably like to take all of that back, but you can't once you say it. He looked very determined to get back on track."

Sanchez, who was vying for his first complete game since his no-hitter against San Diego on July 10, 2009, came out to start the ninth with a 5-0 lead. He gave up a leadoff single to Shane Victorino and was pulled with a 2-0 count on Placido Polanco.

"We needed the game, to start winning again," Sanchez said. "We lost three games in row. To come back is good for us. I was just trying to go out, get ahead and make them swing."

Victorino had the Phillies' first hit, a hard single up the middle in the third.

Pablo Sandoval connected for the Giants, hitting his ninth homer of the season and first while batting right-handed. Last season, he hit 25 homers, six off left-handed pitchers.

The Giants, who had scored three or fewer runs in each of their past five games, jumped on Cole Hamels (7-10) for three in the first. Freddy Sanchez singled and scored on a double by Posey, who is 11-for-25 (.444) in his last six games. Jose Guillen and Juan Uribe added RBI singles later in the inning.

"You get a couple of runs and you start pitching relaxed," Sanchez said.

Hamels had received eight runs of support in his previous six starts combined, including losing consecutive 1-0 decisions. He departed after yielding five runs and seven hits in five innings.

"I wasn't able to hit my spots," Hamels said. "When I finally did, they hit me. You have to get ahead of them. I wasn't able to execute. When you give up three in the first, it's tough to come back. When they put up five, it's really tough."

The Giants needed a win before heading to St. Louis for a three-game series against the Cardinals, in contention in the NL Central.

"Everybody's destiny is in front of them," Bochy said. "It's how you play and you have to win ballgames. It's not about looking for help. You have to take care of business if you're going to get there."

Game notes
The attendance of 45,449 was the highest for the Phillies this season, and their 100th straight sellout. ... Philadelphia's Jimmy Rollins has been safe on 29 straight stolen-base attempts, the best current streak in the majors. ... Giants reliever Santiago Casilla left the team before the game to be with his wife, who went into labor. The team isn't sure whether he will join the team in St. Louis.