Using PHP, PDO to query my games database, to retrieve a game that is available to join. If I use Fetch it will bring back the last row that meets the criteria. I want the first row, so I'm currently using FetchAll then looking at the first row retrieved. Is there a way to use Fetch and tell it to just get the first row. As using FetchAll seems like it is gathering a lot more data than I require.
I'm very new to all of this (animator by trade), but having got my head around old mysql I've now moved over to using PDO which seems great. Slowly building a working asynchronous game server.
Here's my code, which works but I wonder if it could be more efficient, if I did'nt use FetchAll.
//Set variables to use when searching for random game in table
$player2_id = 0; //if 0 then player 2 slot has not been filled.
$whose_turn = 2; //if 2 then its player twos go...so good to join.
//IS THERE A RANDOM GAME TO JOIN
$current_game = $db->prepare("SELECT * FROM games_database WHERE player2_id=? AND whose_turn=?");
$current_game->execute(array($player_2_id, $whose_turn));
//Fetch All the results
$random_game_to_use = $current_game->fetchAll(PDO::FETCH_ASSOC);
//Get the first of all the results
$firstAvailableGame = $random_game_to_use[0];
$rand_game_name = $firstAvailableGame['game_name'];
I hope that makes sense. Any help or pearls of wisdom would be gratefully received. Also just want to say Stackoverflow has been an incredibly helpful website in my learning of coding PHP and PDO database queries. I've probably tried to learn to code about 4 times in my life but finally its starting to stick nicely. This is my first question... thanks Jon.