php - How to use mysql to find a field name of a record? -
pdo sql codes :
while($r = $q->fetch(pdo::fetch_assoc)){ $gg = join('</td><td>', $r); echo "<tr><td>" . $no_count . "</td><td>" . $gg . "</td></tr>"; $no_count = $no_count + 1; } variable $r record, how can echo field name of $r?
let's $r carry records 2 different fields "product" , "price". value of $r "apple", "130". how can add "usd" "130"?
i need like.... if $field_name == "$r['price']" { $r = join('usd', $r); };
thanks mike b, there :
while($r = $q->fetch(pdo::fetch_assoc)){ foreach ($r $name => $value) { if ($name == "price"){ $r = "usd" . $value; // line got problem, how change value in array variable $r? } } $gg = join('</td><td>', $r); echo "<tr><td>" . $no_count . "</td><td>" . $gg . "</td></tr>"; $no_count = $no_count + 1; }
array_keys($r) list of fields table since you're fetching associative array.
you can loop through $r:
foreach ($r $name => $value) { print "$name: " . $value; } update
// line got problem, how change value in array variable $r?
$r[$name] = 'usd' . $value; make edit original name. since have key in $name variable foreach loop can set directly.
Comments
Post a Comment