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;




 ?>

 

Edited by slproject
missread
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.