Mattyspatty Posted March 17, 2006 Share Posted March 17, 2006 $res = mysql_query("SELECT card_id FROM bj ORDER BY RAND()");$draw = mysql_fetch_array($res);$c = 0;$dcard = ",".$draw[$c];$c++;$dcard .= ",".$draw[$c];$c++;$pcard = ",".$draw[$c];$c++;$pcard .= ",".$draw[$c];$c++;echo count($draw);basically im creating a blackjack game, the dealer and player each are drawn 2 cards. but the count only returns 2?!?! its really bugging me, ive tried using LIMIT in the query, to no avail... does anyone have any other suggestions? i need to keep the query so i can keep referring to it, without getting duplicate cards, later in my scriptnote: if my syntax looks 'tacky' i dont care ;) Quote Link to comment https://forums.phpfreaks.com/topic/5170-mysql_fetch_array-not-fetching-enough-rows/ Share on other sites More sharing options...
kenrbnsn Posted March 17, 2006 Share Posted March 17, 2006 What is the structure of your database table? How many fields in a row? How many records?What your code is doing is geting one row at random and accessing (or trying to access) 4 fields in that row, which is probably not what you want.Let's assume your table has 4 fields: rank (A thru K), value (1 thru 10), suit (D,C,S,H), and an autoincrement Id.Here's one way of coding this (not tested -- typed in off the top of my head)[code]<?php$dcards = array();$pcards = array();$q = "SELECT card_id FROM bj ORDER BY RAND()";$rs = mysql_query($q) or die('Problem getting cards from the deck: ' . $q . '<br />' . mysql_error());$cards_dealt = false;while ($rw = mysql_fetch_assoc($rs) && !cards_dealt) { $test_card = $rw['rank'] . $rw['suit'] . '/' . $rw['value']; if (!in_array($test_card, $dcards) && !in_array($test_card,$pcard)) { if (count($dcards) < 2) $dcards[] = $test_card; else if (count($pcards) < 2) $pcards[] = $test_card; } if (count($pcards) == 2 && count($dcards) == 2) $cards_dealt = true;}echo '<pre>Dealer Cards:' . print_r($dcards) . '</pre>';echo '<pre>Player Cards:' . print_r($pcards) . '</pre>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/5170-mysql_fetch_array-not-fetching-enough-rows/#findComment-18349 Share on other sites More sharing options...
Mattyspatty Posted March 17, 2006 Author Share Posted March 17, 2006 thanks for the reply.how i had my query is right, maybe i should have explained the table.it has card_id 1 to 52 and card value 2 to 11.the id is its position in the deck, and value its what card it is. eg card value 4 is a 4,value 11 is an ace, and the J,Q and K are value 10. the suit is of no use.its just not collecting enough rows. i tested the value of $c and it properly increments but then i count $draw and it only returns 2 :S, which i am assuming means its only fetching 2 rows. Quote Link to comment https://forums.phpfreaks.com/topic/5170-mysql_fetch_array-not-fetching-enough-rows/#findComment-18352 Share on other sites More sharing options...
kenrbnsn Posted March 17, 2006 Share Posted March 17, 2006 Each fetch gets one row. Your "count" is getting the number of fields in the returned row, which is 2. In order to get more than one row returned, you need to put the fetch into a while loop. You should be able to adapt my code to fit your database table.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5170-mysql_fetch_array-not-fetching-enough-rows/#findComment-18357 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.