Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Looping through a resultset and executing a second query for each row is bad practice and causes unnecessary load on the database.<br> It looks like you need an <code>INNER JOIN</code>:</p> <pre class="lang-sql prettyprint-override"><code> SELECT t1.* FROM table1 t1 INNER JOIN table2 t2 ON t2.id = t1.data ORDER BY t2.field </code></pre> <p>This way, you can get all the data with <strong>one</strong> single query.</p> <hr> <p><em>EDIT:</em><br> Generally you should try to avoid returning all columns (<code>SELECT *</code>) and select only the columns that you really need:</p> <pre><code>SELECT t1.id, t2.field1, t1.field2, ... </code></pre> <p>This will not only reduce the load on the database (less data to select and to transfer over the network), but it will also help to eliminate double column names because double column names cause problems when you want to display them (as you found out yourself).</p> <p>You can do two things to avoid this problem:</p> <ol> <li><p>if both tables have columns with identical names and identical content (e.g. if these columns are used to join the tables), just select one of them (no matter which one, because they are identical).</p></li> <li><p>if both tables have columns with identical names and different content and you need both of them, the easiest way would be to give one of them an alias:</p> <pre><code>SELECT t1.id, t2.id AS AnotherId, ... </code></pre> <p>This will cause the second column to be named "AnotherId", so you can get it with <code>$rs[AnotherId]</code>.<br> Honestly, I'm no PHP guru so I don't know if PHP understands <code>$rs[table.field]</code> as well, but there are enough data access technologies which have problems when a query returns two columns with identical names...so aliasing duplicate columns can never be wrong.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload