sql >> Database >  >> RDS >> Mysql

SQL-join resulteert in een object in codeigniter

// first, we need the SQL results in the $result_array variable
$sql = 'SELECT ...';  // your SQL command
$result_array = $this->db->query($sql)->result_array();  // codeigniter code

// here the real answer begins
$result = array();

foreach ($result_array as $row)
{
    if (!isset($result[$row['authorid']])
    {
        $author = new StdClass();
        $author->authorid = $row['authorid'];
        $author->authorname = $row['authorname'];
        $author->books = array($row['books']);
        $author->favorited = array($row['favorited']);
        $result[$row['authorid']] = $author;
    }
    else
    {
        if (!in_array($row['books'], $result[$row['authorid']]->books))
        {
            $result[$row['authorid']]->books[] = $row['books'];
        }
        if (!in_array($row['favorited'], $result[$row['authorid']]->favorited))
        {
            $result[$row['authorid']]->favorited[] = $row['favorited'];
        }
    }
}

$result = array_values($result);
echo json_encode($result);



  1. Het aantal kolommen van mysql.proc is onjuist. Verwacht 20, gevonden 16. De tabel is waarschijnlijk beschadigd

  2. Doctrine2:Kan entiteit niet selecteren via identificatievariabelen zonder ten minste één rootentiteitsalias te kiezen

  3. Automatisch aanvullen in de MySQL-opdrachtregelclient

  4. Wat is het equivalent van bind_result op PDO?