mysql - Converting SQL Table ID into another field in PHP -
so i'm trying following work;
//search userpets table user owns $query = "select * userpets owner = '$username'"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); $userspets = $row['petid']; //search pets table list of pet names , details $query = "select * pets"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); $petid = $row['id'] $petname = $row['petname']; $petimg = $row['petimg'] //turn pet id pet name , image echo "pets: ".$userspets;
essentially i'm trying this; 'userpets' table contains 'owned' pets , players username displayed. want grab pets owned user , compare petid 'pets' table. want take pet's name , image table , 'echo' onto page.
getting ids fine, don't know how make convert id's names.
you can use join
of mysql
or foreach
of php
this example using php foreach
$query = "select * userpets owner = '".$username."'"; $result = mysqli_query($conn, $query); $petid = array(); // store petid of user $rows = mysqli_fetch_all($result,mysqli_assoc); foreach($rows $row) { $petid[] = $row['petid']; } $query = "select * pets id in (".implode(",",$petid).")"; // implode convert array string delimete // example array(0=>35, 1=>36, 2=>48) convert "35,36,48" // , above query should : select * pets id in (35,36,48) $result = mysqli_query($conn, $query); $pets = mysqli_fetch_assoc($result); // dump echo "<pre>"; var_dump($pets); echo "</pre>"; die;
using mysql join
<?php $query = "select pet.id, pet.petname, pet.petimg, up.owner pets pet left join userpets on pet.id = up.pet_id up.owner = '".$username."'";
Comments
Post a Comment