I'm trying to extract data from the database. Essence: Get the GET variable (character name), connect to the "characters" base, extract its "char_id" from the variable, connect to the "items" table, and retrieve all the data related to "item_id" and "loc_data" by "owner_id" .. char_id - is in the characters table and has an identical number, like owner_id (but owner_id is in the items table)

public function getCharItem($name){ $db = $this->Connect(self::$_conf['db']); $character = $db->select('characters',array('charId'),array('char_name'=>$name)); $ownerId = ''; $item_list = $db->query("SELECT item_id,loc_data FROM `items` WHERE `owner_id` = ТУТ НУЖНО ПОДСТАВИТЬ CHARID")->fetchAll(); if($item_list !== false and count($item_list) > 0){ foreach($item_list as $key=>$onl){ $ownerId[$key] = array( 'id'=>$onl['item_id'], 'loc_data'=>$onl['loc_data'] ); } if(is_array($ownerId)){ echo $this->XMLRender(array('Characters','Stat','CharBlock'),$ownerId,true); } }else{ echo 0; } } 

I need to get all items_id by ID and output them in xml. But the output from the array is not correct. I ask for help

  • What kind of animal is you using as a Query Builder? Looks like some kind of wrapper over PDO ... - Maxim Timakov

1 answer 1

One request using PDO:

 $sql = <<<SQL select item_id as id, loc_data from `items` where `owner_id` = ( select charId from `characters` where `char_name` = :char_name ) SQL; $item_list = $db->prepare($sql); $item_list->execute([ ':char_name' => $name; ]); $item_list = $item_list->fetchAll(\PDO::FETCH_ASSOC); var_dump($item_list); 

For medoo.in

 $sql = <<<SQL select item_id as id, loc_data from `items` where `owner_id` = ( select charId from `characters` where `char_name` = :char_name ) SQL; // обращаемся к PDO $item_list = $db->pdo->prepare($sql); $item_list->execute([ ':char_name' => $name; ]); $item_list = $item_list->fetchAll(\PDO::FETCH_ASSOC); var_dump($item_list); 
  • Can this be done based on my code? I use the library of medoo, and did not really understand how to use your code ( - Elizaveta
  • @ Elizabeth medoo.in ? then you can use medoo :: $ pdo - replace $db->prepare($sql); on $db->pdo->prepare($sql); - added an example in response - Maxim Timakov