Note that there are some explanatory texts on larger screens.

plurals
  1. POphp multiple mysql commands in array
    primarykey
    data
    text
    <p>Is it possible build an array of SQL commands to query the DB? What I have is three tables, each have columns with amounts due. Would like to select anything that is due and display on screen so it can be invoiced (preferably in a table) and each row with it's respective customers dues.</p> <p>I can select everything that is due using UNION ALL between the three tables, however I cant figure out how to list them by ID in the table row.</p> <p>Below is what I have so far. At this pace I'll have to run each query separately and list them in three separate lists. Suggestions?</p> <pre><code> &lt;table&gt; &lt;tr&gt; &lt;th&gt; ID&lt;/th&gt; &lt;th&gt; Cost 1&lt;/th&gt; &lt;th&gt; Cost 2&lt;/th&gt; &lt;th&gt; Cost 3&lt;/th&gt; &lt;/tr&gt; &lt;?php $list1 = "SELECT ID, Cost FROM Table1 WHERE Invoiced IS NULL;"; //$list2 = "SELECT ID, Price2 FROM Table2 WHERE Expiration BETWEEN '$curDate' AND '$maxDate';"; //$list3 = "SELECT ID, Price3 FROM Table3 WHERE Expiration BETWEEN '$curDate' AND '$maxDate'"; $result = mysql_query($list1, $link) or die(mysql_error()); $num_rows = mysql_num_rows($result); $num_fields = mysql_num_fields($result); for ($i=0; $i&lt;$num_rows; $i++) { for ($j=0; $j&lt;$num_fields; $j++) { $invoice[$i][mysql_fieldname($result,$j)] = mysql_result($result,$i,mysql_field_name($result,$j)); } } //eventually the order it should be listed on screen for($i=0; $i&lt;count($invoice); $i++) { echo "&lt;tr&gt;&lt;td&gt;".$invoice[$i]["ID"]."&lt;/td&gt; &lt;td&gt;".$invoice[$i]["Cost"]."&lt;/td&gt; &lt;td&gt;".$invoice[$i]["Price2"]."&lt;/td&gt; &lt;td&gt;".$invoice[$i]["Price3"]."&lt;/td&gt;&lt;/tr&gt;"; } ?&gt; &lt;/table&gt; </code></pre> <p><strong>Edit after comment:</strong></p> <p>Query being passed and returning syntax error <code>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'all LEFT JOIN table1 ON all.ID = table1.ID LEFT JOIN t' at line 7</code>:</p> <pre><code> $query = " SELECT all.ID, table1.Cost1, table2.Price2, tabl3.Price3 FROM (SELECT ID, Cost1 FROM table1 WHERE Invoiced IS NULL UNION SELECT ID, Price2 FROM table2 WHERE Expiration BETWEEN '$curDate' AND '$maxDate' UNION SELECT ID, Price3 FROM table3 WHERE Expiration BETWEEN '$curDate' AND '$maxDate') AS all LEFT JOIN table1 ON all.ID = table1.ID LEFT JOIN table2 ON all.ID = table2.ID LEFT JOIN table3 ON all.ID = table3.ID "; </code></pre>
    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.
 

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