slproject Posted October 15, 2018 Share Posted October 15, 2018 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); Quote Link to comment Share on other sites More sharing options...
Barand Posted October 15, 2018 Share Posted October 15, 2018 What is in your $search variable? Quote Link to comment Share on other sites More sharing options...
slproject Posted October 15, 2018 Author Share Posted October 15, 2018 Just now, Barand said: What is in your $search variable? At the moment it get its value from a text field input ($_GET). Quote Link to comment Share on other sites More sharing options...
Barand Posted October 15, 2018 Share Posted October 15, 2018 Does it contain any SQL wildcard characters (%) LIKE without wildcards behaves like "=" EG SELECT whatever FROM mytable WHERE name LIKE 'John%' Quote Link to comment Share on other sites More sharing options...
slproject Posted October 15, 2018 Author Share Posted October 15, 2018 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 15, 2018 Share Posted October 15, 2018 Have you tried running the query from the command line to see if you get any hits? Quote Link to comment Share on other sites More sharing options...
slproject Posted October 15, 2018 Author Share Posted October 15, 2018 Sorry, not that good at the command line stuff ? If anyone want to try or check the database it can be found here:http://www.student.bth.se/~kear18/dbwebb-kurser/htmlphp/me/kmom05/me5/db/family.sqlite Quote Link to comment Share on other sites More sharing options...
Barand Posted October 15, 2018 Share Posted October 15, 2018 My mother told me not to accept files from strangers. Quote Link to comment Share on other sites More sharing options...
slproject Posted October 15, 2018 Author Share Posted October 15, 2018 1 minute ago, Barand said: My mother told me not to accept files from strangers. I totally respect that Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted October 15, 2018 Share Posted October 15, 2018 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.) Quote Link to comment Share on other sites More sharing options...
slproject Posted October 15, 2018 Author Share Posted October 15, 2018 (edited) 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 October 15, 2018 by slproject missread Quote Link to comment Share on other sites More sharing options...
slproject Posted October 15, 2018 Author Share Posted October 15, 2018 It seems I just solved it It was the isset() that was giving me trouble... Without it, it works fine. Thank you all! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.