Hi Everyone, I have the following piece of code & I am trying to use to create an array of "Playing Cards" from a mysql db.
I am just not sure how to one single complete array !
Thanks for looking and replying with your suggestions
Hi Everyone, I have the following piece of code & I am trying to use to create an array of "Playing Cards" from a mysql db.
I am just not sure how to one single complete array !
Thanks for looking and replying with your suggestions
Two things were masking the real cause of the repeated output. was right that the foreach/print was in the wrong place, but the six-fold repeat came from iterating a row that contains duplicate keys (numeric + associative) and from reusing the same variable name for both the collector array and the fetched row. Moving the echo out of the inner loop fixed the symptom; the deeper fixes are to fetch only the keys you want and to use distinct variable names.
Practical fixes:
MYSQL_ASSOC or MYSQL_NUM; with mysqli/PDO use the associative fetch mode.foreach if you are printing numeric indexes; iterate once per row (or build a collector array and print after the loop).$row for the fetched row and $cards for the array).Example pattern (mysqli) that avoids the duplicate-print problem:
$mysqli = new mysqli('host','user','pass','db');
$res = $mysqli->query("SELECT id, suit, card FROM cards2 WHERE ...");
$cards = array();
while ($row = $res->fetch_assoc()) {
$cards[] = $row; // collect structured rows
}
foreach ($cards as $c) {
echo $c['id'].' '.$c['suit'].' '.$c['card']."\n";
} Quick debug tips: inside the while, run var_dump($row) or print_r(array_keys($row)) to see whether you have both numeric and associative keys. That will quickly show why a foreach ran 6 times for a 3-column row.
Jump to Post— pritaeas 2,276You should close the while loop, then do the foreach. The foreach is inside the while loop, causing this effect.
here is the php for the above question
$sth = mysql_query("SELECT * FROM cards2 where suit='clubs'");
$rows = array();
$i=0;
while($r = mysql_fetch_assoc($sth)) {
$i++;
array_push($rows, $r["suit"]);
array_push($rows, $r["card"]);
foreach($rows as $key => $value)
{
}
print_r($rows);
the array is displaying, but it is looping through from the start again for each card
Array ( [0] => clubs ) Array ( [0] => clubs [1] => clubs ) Array ( [0] => clubs [1] => clubs [2] => clubs )
You should close the while loop, then do the foreach. The foreach is inside the while loop, causing this effect.
Hi thanks for replying -
But for some reason, I am getting each result 6 times,
the array is printing out like this
1 clubs 8
1 clubs 8
1 clubs 8
1 clubs 8
1 clubs 8
1 clubs 8
When it should only be printing 1 clubs 8
Can anyone help point me in the right direction why this is happening
here is the code I am using -
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////
$sth = mysql_query("SELECT id, suit, card FROM cards2 where id='1' or id='2' or id='3' or id='4' ORDER BY RAND() LIMIT 3");
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// result
$clubresult = mysql_query($query) or die ("no query");
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// array
$rows = array();
while($rows = mysql_fetch_array($sth)) {
//foreach($rows as $key => $value)
foreach($rows as $key => $value){
echo $rows[0]." ".$rows[1]." ".$rows[2]."<br />";
}
Thanks
Hi, Sorry... But this is now fixed, I was echoing out in the wrong place -
The fix, if anyone is reading this is below -
}
echo $rows[0]." ".$rows[1]." ".$rows[2]."<br />";
}
Here is the problem code I was using
echo $rows[0]." ".$rows[1]." ".$rows[2]."<br />";
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.