Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd highly recommend you use PDO, here's a sample using PDO.</p> <pre><code>&lt;?php $dsn = 'mysql:dbname=so;host=localhost'; $user = 'root'; $password = ''; // try to connect. try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e-&gt;getMessage(); exit(1); } // the sql which gets all employees for this company. $getEmployeesSQL = ''; $getEmployeesSQL .= ' SELECT c.name AS company_name, e.name AS employee_name, e.salary, c.location '; $getEmployeesSQL .= ' FROM company c'; $getEmployeesSQL .= ' LEFT JOIN employee e'; $getEmployeesSQL .= ' ON c.id = e.company_id'; $getEmployeesSQL .= ' WHERE c.name = :cname'; // sql to get company data. $companyDataSQL = "SELECT * FROM company WHERE name = :cname"; // prepare the sql. $stmtOne = $dbh-&gt;prepare($getEmployeesSQL); $stmtTwo = $dbh-&gt;prepare($companyDataSQL); // execute $stmtOne-&gt;execute(array(':cname' =&gt; $_GET['company'])); $stmtTwo-&gt;execute(array(':cname' =&gt; $_GET['company'])); // print results. $employees = $stmtOne-&gt;fetchAll(PDO::FETCH_ASSOC); $companyData = $stmtTwo-&gt;fetchAll(PDO::FETCH_ASSOC); /* * ****************************************** */ /* Iterate and print HTML table. */ /* * ****************************************** */ $html = '&lt;table border="1"&gt;'; foreach ($companyData as $company) { foreach ($company as $field =&gt; $value) { $html.= "&lt;tr&gt;&lt;td&gt;$field&lt;/td&gt;&lt;td&gt;$value&lt;/td&gt;&lt;/tr&gt;"; } } $html . '&lt;/table&gt;'; echo $html; $html = '&lt;table border="1"&gt;'; foreach ($employees as $row) { $html.= '&lt;tr&gt;&lt;td&gt;' . implode('&lt;/td&gt;&lt;td&gt;', array_values($row)) . '&lt;/td&gt;&lt;/tr&gt;'; } $html . '&lt;/table&gt;'; echo $html; </code></pre> <p>What this snippet does is that it prints the company data in a table and then gets employee data using company name.</p> <p>The table structure I'm using is as follows:</p> <pre><code>CREATE TABLE `company` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, `location` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 CREATE TABLE `employee` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(200) NOT NULL, `salary` bigint(12) DEFAULT NULL, `company_id` bigint(20) NOT NULL, PRIMARY KEY (`id`), KEY `fk_company` (`company_id`), CONSTRAINT `fk_company` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=latin1 </code></pre> <p>This returns data as follows: (Not pretty, but this'll have to do for now.)</p> <p><img src="https://i.stack.imgur.com/Zqfhr.png" alt="Result"></p>
    singulars
    1. This table or related slice is empty.
    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