Jump to content

[SOLVED] SQLite Keyword Querying


Delaran

Recommended Posts

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  ;D)

 

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

Thanks Keeb! Your post was very helpful for my silly noobie self  :D

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.