PHP MySQL Query Use OR
From w3cyberlearnings
Contents |
PHP MySQL Query Use OR operator
The OR operator uses to filter record and it returns records if either the first condition or the second condition is true.
Syntax
SELECT em.id, em.name, em.grade FROM employee em WHERE em.name='xyx' OR em.name='abc'
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 = array('Mark','Alex'); $sel_query = " SELECT em.id, em.name, em.grade FROM employee em WHERE em.name='{$search[0]}' OR em.name='{$search[1]}'"; $result = mysql_query($sel_query, $connection) or die(mysql_error($connection)); echo '<table border="1">'; echo '<tr><th>id</th><th>Name</th><th>Grade</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); ?>