Getting no matching results for query mysql php -
this question has answer here:
i trying find matching results query in database , put in table form made, no matching results back. ran query in phpmyadmin , got desired output. used var_dump on $result variable , following: resource(4) of type (mysql result). here code:
<?php function rendersearchform($search, $search_by, $search_by, $error){ ?> <!doctype html5> <head> <title>search query</title> </head> <body> <?php //display errors if($error != ''){ echo '<div style="padding:4px; border:1px solid red; color:red;">' . $error . '</div>'; } ?> <form action="" method="post"> <div> <p>search for:</p> <strong>name: *</strong> <input type="text" name="search" value="<?php echo $search; ?>" /><br/> <p>search by:</p> <input type="radio" name="search_by" <?php if (isset($search_by) && $search_by=="firstname") echo "checked";?> value="firstname"/>firstname* <input type="radio" name="search_by" <?php if (isset($search_by) && $search_by=="surname") echo "checked";?> value="surname"/>surname* <br><br> <input type="submit" name="submit" value="submit"> <p>* required</p> </div> </form> </body> </html> <?php } //connect db include('connect_db.php'); //check if submitted if (isset($_post['submit'])) { $search = mysql_real_escape_string(htmlspecialchars($_post['search'])); $search_by = mysql_real_escape_string(htmlspecialchars($_post['search_by'])); //check if search empty if (empty($_post["search"])) { $error = "error: name required"; //error, display form rendersearchform($search, $search_by, $search_by, $error); } elseif // check if name contains letters , whitespace (!preg_match("/^[a-za-z ]*$/",$search)) { $error = "error: letters , white space allowed"; //error, display form rendersearchform($search, $search_by, $search_by, $error); } //check if radio button selected elseif (empty($_post["search_by"])) { $error = "error: search_by required"; //error, display form rendersearchform($search, $search_by, $search_by, $error); }else{ //save data $query = "select * members '$search_by' '%$search%'"; $result = mysql_query($query) or die(mysql_error()); var_dump($result); //display data db echo "<table border='1' cellpadding='10'>"; echo "<tr> <th>id</th> <th>firstname</th> <th>surname</th> <th>telephone</th> <th>cell</th> <th>address</th> <th></th> <th></th> </tr>"; //loop through results of db , display in table while ($row = mysql_fetch_array($result)) { //echo contents in table echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['firstname'] . "</td>"; echo "<td>" . $row['surname'] . "</td>"; echo "<td>" . $row['telephone'] . "</td>"; echo "<td>" . $row['cellphone'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td><a href='edit.php?id=" . $row['id'] . "'>edit</a></td>"; echo "<td><a href='delete.php?id=" . $row['id'] . "'>delete</a></td>"; echo "</tr>"; } echo "</table>"; //this counts number or results $anymatches=mysql_num_rows($result); if ($anymatches == 0) { echo "sorry, can not find entry match query<br><br>"; } //and remind them searched echo "<b>searched for:</b> " .$search. "<b> in: </b>" .$search_by; } }else{ //if form not submitted, display form rendersearchform('','','',''); } ?>
you need remove single quotes between $search_by. try this:
"select * members $search_by '%$search%'"
Comments
Post a Comment