php - Get data except for data in a column -
so have following code:
require_once('db.php'); $getusers = mysqli_query($db, 'select * users'); $rows = []; while ($r = mysqli_fetch_assoc($getusers)) { $rows[] = $r; $getskills = mysqli_query($db, "select * skills id = '" . $r['id'] . "'"); while($r = mysqli_fetch_assoc($getskills)) { $rows['skills'] = $r; } } print(json_encode($rows));
which outputs:
[{"id":"1","name":"user1","skills":{"woodcutting":"6","mining":"10"}},{"id":"2","name":user2"}]
there 2 problems:
- i'd of data in table
skills
except id, or at-least cut off before encoding json. - for reason can't "skills" shown after first user. user2 should have skills object.
what doing wrong?
to columns except id
table skills
, can either list columns want select, this:
mysqli_query($db, "select column1, column2, another_column `skills` id = '" . $r['id'] . "'");
or, can select
, use unset()
weed out id
column before json-encoding:
$getskills = mysqli_query($db, "select * skills id = '" . $r['id'] . "'"); while ($r = mysqli_fetch_assoc($getskills)) { unset($r['id']); // whatever want do. }
the skills
not shown except last user (?) because re-assigning in every iteration of while
loop. here can instead:
require_once('db.php'); $getusers = mysqli_query($db, 'select * users'); $rows = array(); while ($r = mysqli_fetch_assoc($getusers)) { $skills = array(); $temprow = $r; $getskills = mysqli_query($db, "select * skills id = '" . $r['id'] . "'"); while ($r = mysqli_fetch_assoc($getskills)) { unset($r['id']); // since don't want `id`. $skills[] = $r; } $temprow['skills'] = $skills; $rows[] = $temprow; } print(json_encode($rows));
hope helps :)
Comments
Post a Comment