php - How to select DISTINCT using WHERE and OR -
i new php , have searched google, other forums, web site , looked @ several books cannot see how this. please not castigate me ignorance. want select distinct entries field (category) based on criteria field (code) , display drop down list containing distinct categories. field named 'code' contains 1 of 3 data values, being 1, 2, or 4. want select distinct category if code field has '2' or '4', not if contains value '1'.
i have tried several ways of doing confirmation database connection worked drop down box below has no content.
my current code after many trials is:
if ( ! $_post['submit'] ) { // form not submitted, display form input user echo '<form method="post" id="form_id" action="' . htmlspecialchars($_server["php_self"]) . '">'; try { $dbh = new pdo("mysql:host=$hostname;dbname=gosdirect", $username, $password); $code2 = '2'; $code4 = '4'; $sql = $dbh->prepare("select distinct category directory code =$code2 or code =$ode4 order category asc"); $sql->execute(); // display message if connection ok echo ' connected database<br />'; echo 'i looking local: <select name="category">'; while ($row = $sql->fetch(pdo::fetch_assoc)) { $category = $row['category']; echo '<option value="' . $category . '">' . $category . '</option>'; } echo '</select>'; // close database connection $dbh = null; } catch(pdoexception $e) { echo $e->getmessage(); } echo '<input id="form_submit" type="submit" name="submit" value="search" /></form>'; ?>
if use line:
$sql = $dbh->prepare("select distinct category directory order category asc");
i drop down box populated category names, includes categories have value of '1' in 'code' field , don't want that, assuming there fundamentally wrong 'select request.
here image of get:
screen shot http://www.wigweb.biz/gosport-error.jpg
i appreciate on this.
many thanks
tog porter
it matches codes have 2 or 4 in , exclude have 1 in them
select distinct category directory (code '%2%' or code '%4%') , not (code '%1%') order category asc
also if want use variable in sql varchar
filed surround quote '$code2'
Comments
Post a Comment