Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL + PHP - Select info based on previous select result
    primarykey
    data
    text
    <p>I'm doing an integration of PHP and MySQL. The following code is working fine:</p> <pre><code>&lt;?php include_once("conf.php"); $sql = "SELECT Name,Address FROM customers"; $rs = mysqli_query($connect,$sql) or die("can't connect to DB"); $temp = mysqli_num_rows($rs); while($row = mysqli_fetch_array($rs)) { $convert = mb_convert_encoding ($row['Name'],"UTF-8","gbk"); $print .= "&lt;tr&gt;&lt;td&gt;".$convert."&lt;/td&gt;&lt;td&gt;".$row['Address']."&lt;/td&gt;&lt;td&gt;".$sql2."&lt;/td&gt;&lt;/tr&gt;\n"; } </code></pre> <p>This code is working, but now I need to select specific information from ANOTHER table, BASED on the result (Name) of this one.</p> <p>So, this would be the other MySQL select:</p> <pre><code>$sql2 = "select History from Delivery where Name="$convert"; </code></pre> <p>Based on the <strong>"Name"</strong> of my customer (from customer table), I need to get his <strong>"History"</strong> at the <strong>Delivery</strong> table.</p> <p>So, I will print his: Name + Address + History</p> <p>What would be the best solution??</p> <p>@@@@@@@@@ SOLVED @@@@@@@@@@</p> <pre><code>$sql = 'SELECT * FROM customers c INNER JOIN Delivery d ON c.Name = d.Name'; $result = mysqli_query($connect, $sql); $temp = mysqli_num_rows($rs); while($row = mysqli_fetch_array( $result)) { $convert = mb_convert_encoding ($row['Name'],"UTF-8","gbk"); $print .= "&lt;tr&gt;&lt;td&gt;".$convert."&lt;/td&gt;&lt;td&gt;".$row['Address']."&lt;/td&gt;&lt;td&gt;".$row['History']."&lt;/td&gt;&lt;/tr&gt;\n"; } </code></pre> <p>My current situation is: I have three tables from which I need to get info.</p> <p>Tables: customers, users, history.</p> <p>From customers I need ammount of customers.</p> <p>From Delivery I need History (based on ammount of customers).</p> <p>From users I need Name (actually it's almost a nickname).</p> <p><strong>Since that these three tables have a 'Name' column and I CAN'T change column names, how can I get the Name from USERS instead of CUSTOMERS or DELIVERY using that INNER JOIN statement??</strong></p>
    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. 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