Delaran Posted April 24, 2007 Share Posted April 24, 2007 Hey guys, I must say this forum has to be the best resource on the net for getting help with PHP scripts. Y'all helped me quite a bit yesterday and I was able to program at least two more pages -- no problems. Trying to figure this stuff out on your own is -tough- (for me at least ) Today I've got another issue. This page works, but I'm having issues with it not retrieving data unless you spell the keyword exactly how it is put into the database. For instance, trying to query, "crystal", knowing that, "Crystal necklace circa 1940" is in the database returns no results. However, typing in the exact "Crystal necklace circa 1940" returns what I'm looking for. The page I'm programming is supposed to be fairly user friendly, so I'd like it to return results even if people don't enter the entire string of text in the way it was submitted to us. In any case, this is my code so far, perhaps someone can shed some light onto a command that I might add in or modify one of the ones I already have? Thanks guys -- you rock. <html> <head> <title>Database keyword search</title> </head> <center> <?php // set path of database file $db = $_SERVER['DOCUMENT_ROOT']."/../wantdb.sqlite2"; // open database file $handle = sqlite_open($db) or die("Could not open database"); { // generate and execute query $keyword = sqlite_escape_string($_POST['keyword']); $query = "SELECT * from wantdb WHERE item='$keyword'"; $result = sqlite_query($handle, $query) or die("Error in query: ".sqlite_error_string(sqlite_last_error($handle))); if (sqlite_num_rows($result) > 0) { echo "<table cellpadding=10 border=1>"; while($row = sqlite_fetch_array($result)) { echo "<tr>"; echo "<td>".$row[0]."</td>"; echo "<td>".$row[1]."</td>"; echo "<td>".$row[2]."</td>"; echo "<td>".$row[3]."</td>"; echo "<td>".$row[4]."</td>"; echo "</tr>"; } echo "</table>"; } } // all done // close database file sqlite_close($handle); ?> Link to comment https://forums.phpfreaks.com/topic/48487-solved-sqlite-keyword-querying/ Share on other sites More sharing options...
keeB Posted April 24, 2007 Share Posted April 24, 2007 Change $query = "SELECT * from wantdb WHERE item='$keyword'"; to $query = "SELECT * from wantdb WHERE item like '*$keyword'*"; Link to comment https://forums.phpfreaks.com/topic/48487-solved-sqlite-keyword-querying/#findComment-237094 Share on other sites More sharing options...
Delaran Posted April 24, 2007 Author Share Posted April 24, 2007 Thanks Keeb! Your post was very helpful for my silly noobie self I actually replaced the $query = "SELECT * from wantdb WHERE item like '*$keyword'*"; with $query = "SELECT * from wantdb WHERE item like '%$keyword%'"; and it worked like a charm. Link to comment https://forums.phpfreaks.com/topic/48487-solved-sqlite-keyword-querying/#findComment-237103 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.