CodeIgniter/PHP/MySQL: Retrieving data with JOIN -
I am new to PHP / MySQL and super-new to CodeIgniter. I have information in many MySQL tables. I want to recover it with JOIN, where the primary key is equal to $ variable ... how can I do it and get all the fields without the primary area? ?
What I am doing now is this (only two tables are included here):
function getAll ($ id) {$ this- & gt; Db- & gt; Choose ('*'); $ This- & gt; Db- & gt; ('Film'); $ This- & gt; Db- & gt; Join ('Poster', 'movies.id = posters.id'); // WHERE id = $ id ... goes somewhere ... $ q = $ this- & gt; Db- & gt; get receive (); If ($ q-> num_rows () == 1) {$ row = $ q- & gt; Line (); $ Data = array ('id' = & gt; $ row-> ID, 'title' => $ row-> title, 'year' => $ row-> Year, 'Runtime' = & gt; $ line- & gt; runtime, 'plotlineline' => gt; line line & gt; plotline line, 'poster_ril' = & gt; $ row-> poster_rill); } $ Q- & gt; Free_race (); $ Return data; ID (PK), title, year, runtime and plotoutline are columns from the first table and poster_arral is the field from the second table. The second table also includes an ID (PK) column which I do not want to recover because I already have it.
John is right here is an example:
$ this- > Db-> select ('movies.id, movies.title, movies.year, movies.runtime as totaltime, posters.poster_url'); $ This- & gt; Db- & gt; ('Film'); $ This- & gt; Db- & gt; Join ('Poster', 'movies.id = posters.id'); $ This- & gt; Db- & gt; Where ('movies.id', $ id); $ Q = $ this- & gt; Db- & gt; get receive (); This will return those items that have -> ID, -> title, -> year, -> time, and -> poster_reality properties. You will not need additional code to get data from each line
Do not forget, if active record syntax becomes a bit impractical, you can use full SQL queries and get similar results:
$ Sql = "SELECT movies.id, movies.title, movies.year, movies.runtime as totaltime, posters.poster_url Join poster on INNER movies from films iid = posters.id WHERE Movies.id =?" Return $ the- > Db->; ($ Sql, array ($ id)) - & gt; result (); Both forms will ensure that your data runs away properly.
Comments
Post a Comment