Jump to content


Photo

mysql_fetch_array not fetching enough rows!


  • Please log in to reply
3 replies to this topic

#1 Mattyspatty

Mattyspatty
  • Members
  • PipPipPip
  • Advanced Member
  • 76 posts
  • LocationEngland :D

Posted 17 March 2006 - 02:26 PM

$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 script

note: if my syntax looks 'tacky' i dont care ;)
ok, so my PHP is a bit rusty... my replies are 90% ok.
the other 10% either have syntax errors or are the "hard solution"
http://www.php.net  http://www.google.com - Are your friends!

I have a C++ problem with 'keybd_event' in certain applications. if you can help me PM me! :P
Needs motivation to create another quick cash website. I need a new TV

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 March 2006 - 02:59 PM

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)
<?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>';
?>

Ken


#3 Mattyspatty

Mattyspatty
  • Members
  • PipPipPip
  • Advanced Member
  • 76 posts
  • LocationEngland :D

Posted 17 March 2006 - 03:05 PM

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.
ok, so my PHP is a bit rusty... my replies are 90% ok.
the other 10% either have syntax errors or are the "hard solution"
http://www.php.net  http://www.google.com - Are your friends!

I have a C++ problem with 'keybd_event' in certain applications. if you can help me PM me! :P
Needs motivation to create another quick cash website. I need a new TV

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 March 2006 - 03:12 PM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users