I-AM-OBODO Posted August 26, 2008 Share Posted August 26, 2008 I have users in my database, but i want to be able to perform a search by a criteria eg username or transaction ref, how do i go about that, cos i tried using select where, but it seems not working. i need a better way if my way is not good. thankz here is what i did: <?php include_once("../includes/connection.php"); ?> <?php $result = mysql_query( "SELECT * FROM easylife WHERE Firstname LIKE '%$keyword'" ) or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); while ($get_info = mysql_fetch_row($result)){ foreach ($get_info as $field) print "</tr>\n"; } print "</table>\n"; mysql_close($link); ?> <style type="text/css"> <!-- .style1 { font-family: Georgia, "Times New Roman", Times, serif; font-size: 14px; font-weight: bold; } --> </style> <br> <form method="POST" action="choice2.php"> <span class="style1">Keyword</span> <input name="keyword" type="text" id="keyword" size="25"> <input name="Submit" type="submit" value="Search"> <input type="reset"> </form> </body> </html> and then.... <?php $keyword=$_REQUEST['keyword']; $conn = mysql_connect("localhost","user","pasword"); mysql_select_db("database_db",$conn); $sql = "SELECT * FROM easylife WHERE Firstname LIKE '%$keyword%'"; $result = mysql_query($sql, $conn) or die(mysql_error()); while ($newArray = mysql_fetch_array($result)) { $txnref = $newArray['txnref']; $Beneficiary = $newArray['Beneficiary']; $amount = $newArray['amount']; $PaymentType = $newArray['PaymentType']; $Address = $newArray['Address']; $StartDate = $newArray['StartDate']; $EndDate = $newArray['EndDate']; $Firstname = $newArray['Firstname']; $Middlename = $newArray['Middlename']; $Surname = $newArray['Surname']; $OfficialAddress = $newArray['OfficialAddress']; $BillingAddress = $newArray['BillingAddress']; $Email = $newArray['Email']; $Phone = $newArray['Phone']; $date_of_order = $newArray['date_of_order']; } ?> <style type="text/css"> <!-- .style1 { color: #000066; font-weight: bold; } --> </style> <a href="select.php">Home</a> | <a href="logout.php">Logout</a> <p> <h3>Client's Information</h3> <table width="351" border="2" cellpadding="1" cellspacing="0" bordercolor="#000000" style="border-collapse:collapse "> <tr> <td width="104"><strong>Trans Ref</strong></td> <td width="231"><span class="style1"> <label> <?php echo $txnref ?> </label> </span></td> </tr> <tr> <td><strong>Beneficiary </strong></td> <td><span class="style1"> <label> <?php echo $Beneficiary ?> </label> </span></td> </tr> <tr> <td><strong>Amount</strong></td> <td><span class="style1"> <label> <?php echo $amount ?> </label> </span></td> </tr> <tr> <td><strong>Payment Type</strong></td> <td><span class="style1"> <label> <?php echo $PaymentType?> </label> </span></td> </tr> <tr> <td><strong>Address</strong></td> <td><span class="style1"> <label> <?php echo $Address?> </label> </span></td> </tr> <tr> <td><strong>Start Date</strong></td> <td><span class="style1"> <label> <?php echo $StartDate ?> </label> </span></td> </tr> <tr> <td><strong>End Date</strong></td> <td><span class="style1"> <label> <?php echo $EndDate ?> </label> </span></td> </tr> <tr> <td><strong>Firstname</strong></td> <td><span class="style1"><?php echo $Firstname ?></span></td> </tr> <tr> <td><strong>Middlename</strong></td> <td><span class="style1"><?php echo $Middlename ?></span></td> </tr> <tr> <td><strong>Sastname</strong></td> <td><span class="style1"><?php echo $Surname ?></span></td> </tr> <tr> <td><strong>Official Address </strong></td> <td><span class="style1"><?php echo $OfficialAddress ?></span></td> </tr> <tr> <td><strong>Billing Address </strong></td> <td><span class="style1"><?php echo $BillingAddress ?></span></td> </tr> <tr> <td><strong>Email</strong></td> <td><span class="style1"><?php echo $Email ?></span></td> </tr> <tr> <td><strong>Phone</strong></td> <td><span class="style1"><?php echo $Phone ?></span></td> </tr> <tr> <td><strong>Date/Time Ordered </strong></td> <td><span class="style1"> <label for="register"><?php echo $date_of_order ?></label> </span></td> </tr> </table> <p> Quote Link to comment https://forums.phpfreaks.com/topic/121396-search-help/ Share on other sites More sharing options...
Fadion Posted August 26, 2008 Share Posted August 26, 2008 As you have a lot of code there, im breaking it down to a simple example of your case. It uses WHERE LIKE, like you did, but maybe you're doing something wrong so give it a look: <?php if(isset($_POST['keyword'])){ //this comes from the search input $search = mysql_real_escape_string($_POST['keyword']); $results = mysql_query("SELECT * FROM easylife WHERE firstname LIKE '%$search%'"); //or '%$search' if you want it to start with anything, or '$search%' to end with anything if(mysql_num_rows($results) != 0){ while($values = mysql_fetch_array($results)){ echo $values['firstname']; //echo all the other fields } } else{ echo 'No results found'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/121396-search-help/#findComment-625945 Share on other sites More sharing options...
r-it Posted August 26, 2008 Share Posted August 26, 2008 $result = mysql_query( "SELECT * FROM easylife WHERE Firstname LIKE '%$keyword%'" ) or die("SELECT Error: ".mysql_error()); That should work Quote Link to comment https://forums.phpfreaks.com/topic/121396-search-help/#findComment-625947 Share on other sites More sharing options...
I-AM-OBODO Posted August 28, 2008 Author Share Posted August 28, 2008 Thanks all for your help. Finaly i gat it done and working. This is how i did it. <?php mysql_connect ("localhost", "name","password") or die (mysql_error()); mysql_select_db ("database"); $keyword = $_POST['keyword']; if ( $keyword == '' ) { die("Query failed. Ensure you gave a criteria");} $sql = mysql_query("SELECT * FROM easylife WHERE Firstname LIKE '%$keyword%' OR Middlename LIKE '%$keyword%' "); while ($row = mysql_fetch_array($sql)){ echo 'Transaction Ref: '.$row['txnref']; echo '<br/> Beneficiary: '.$row['Beneficiary']; echo '<br/> Amount: '.$row['amount']; echo '<br/> PaymentType: '.$row['PaymentType']; echo '<br/> Address: '.$row['Address']; echo '<br/> Firstname: '.$row['Firstname']; echo '<br/> Surname: '.$row['Surname']; echo '<br/> Middlename: '.$row['Middlename']; echo '<br/> Phone: '.$row['Phone']; echo '<br/><br/>'; } ?> but the funny thing is that if no criteria was provided on the on the search box, it brings out all the entry in the database. how do i stop that now, cos my validation is not okay. but am still trying something. help will be appreciated thanks all once again Quote Link to comment https://forums.phpfreaks.com/topic/121396-search-help/#findComment-627921 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.