Jump to content

Help with sqlite


slproject

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

 

Link to comment
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;




 ?>

 

Link to comment
Share on other sites

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.