Jump to content

mysql_fetch_array not fetching enough rows!


Mattyspatty

Recommended Posts

$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 ;)
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.