Jump to content
slproject

Help with sqlite

Recommended Posts

Hi! I am trying to learn using sqlite and want to create a simple searchengine... I can't figure out what i am doing wrong.

$database2 contains the path to the db file and $search contains the word to look for. $result will hold the searchresults.
But, I don't get any hits doing this. The database is correct and connects fine.

Please help :)
 

    $fileName = __DIR__ . $database2;
    $dsn = "sqlite:$fileName";

    try {
          $db = new PDO($dsn);
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
          echo "Failed to connect to the database using DSN:<br>$dsn<br>";
          throw $e;
    }

    $sql = "SELECT * FROM familyMembers WHERE Name LIKE ? OR Relation LIKE ? OR Age LIKE ?";
    $stmt = $db->prepare($sql);

    $params = [$search, $search, $search];
    $stmt->execute($params);

    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

 

Share this post


Link to post
Share on other sites
Just now, Barand said:

What is in your $search variable?

At the moment it get its value from a text field input ($_GET).

Share this post


Link to post
Share on other sites

Does it contain any SQL wildcard characters (%)

LIKE without wildcards behaves like "="

EG

SELECT whatever FROM mytable WHERE name LIKE 'John%'

Share this post


Link to post
Share on other sites
2 minutes ago, Barand said:

Does it contain any SQL wildcard characters (%)

LIKE without wildcards behaves like "="

EG

SELECT whatever FROM mytable WHERE name LIKE 'John%'

I have tried with and without wildcards, no results :(

Share this post


Link to post
Share on other sites

Have you tried running the query from the command line to see if you get any hits?

Share this post


Link to post
Share on other sites
1 minute ago, Barand said:

My mother told me not to accept files from strangers.

I totally respect that :)

Share this post


Link to post
Share on other sites

it would be nice if you posted your code that's setting the $search variable (you could be doing something that you think is correct, but isn't) and your code using $result (you could be doing something that you think is correct, but isn't.)

Share this post


Link to post
Share on other sites
12 minutes ago, mac_gyver said:

it would be nice if you posted your code that's setting the $search variable (you could be doing something that you think is correct, but isn't) and your code using $result (you could be doing something that you think is correct, but isn't.)


<center><img src=img/glogo.png width=300>

<form action=search.php method=GET>
<input type=text size=30 name=search value=<?= $_GET['search'] ?>>
<input type="submit" value="Search">

<?php

$search = isset($_GET['search'])
    ? $_GET['search']
    : null;

    if (is_null($search)) {
        exit("<p>Nothing to display, please enter a searchstring.");
    }

    $fileName = __DIR__ . $database2;
    $dsn = "sqlite:$fileName";

    try {
          $db = new PDO($dsn);
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
          echo "Failed to connect to the database using DSN:<br>$dsn<br>";
          throw $e;
    }

    $sql = "SELECT * FROM familyMembers WHERE Name LIKE ? OR Relation LIKE ? OR Age LIKE ?";
    $stmt = $db->prepare($sql);

    $params = [$search, $search, $search];
    $stmt->execute($params);

    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);

$rows = null;
foreach ($result as $row) {

    echo "<p>|$row|</p>";

    $rows .= "<tr>";
    $rows .= "<td>{$row['Name']}</td>";
    $rows .= "<td>{$row['Age']}</td>";
    $rows .= "<td>{$row['Relation']}</td>";
    $rows .= "</tr>\n";
}

echo <<<EOD
<table>
<tr>
  <th>Name</th>
  <th>Age</th>
  <th>Relation</th>
</tr>
$rows
</table>
EOD;




 ?>

 

Edited by slproject
missread

Share this post


Link to post
Share on other sites

It seems I just solved it :)  It was the isset() that was giving me trouble... Without it, it works fine. Thank you all!


 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.