PHP MySQL Query Use AND
From w3cyberlearnings
Contents |
PHP MySQL AND Operator for query multiple table
The AND Operator uses to filter records and it returns the query records only when the first and the second condition are true.
Syntax
SELECT em.name, em.grade, sal.salary FROM employee em, employee_salary sal WHERE em.id=sal.employee_id AND sal.employee_id IN(1,2,3,5);
Example 1
<?php define('HOST', 'localhost'); define('USER', 'root'); define('PASS', 'yeething'); define('DBNAME', 'woowood'); $connection = mysql_connect(HOST, USER, PASS) or die("can not connect to the server!<br/>"); $rdb = mysql_select_db(DBNAME) or die("The " . DBNAME . "database could not be selected"); // Search employee id $str_search = '1,2,3,5'; // SQL Query $sel_query = " SELECT em.name, em.grade, sal.salary FROM employee em, employee_salary sal WHERE em.id=sal.employee_id AND sal.employee_id IN({$str_search})"; $result = mysql_query($sel_query, $connection) or die(mysql_error($connection)); echo '<table border="1">'; echo '<tr><th>id</th><th>First Name</th><th>Last Name</th></tr>'; while ($row = mysql_fetch_assoc($result)) { echo '<tr>'; foreach ($row as $value) { echo '<td>' . $value . '</td>'; } echo "</tr>"; } echo '</table>'; mysql_close($connection); ?>