I'm trying to write a search engine using pdo/php but I am a beginner still in programming and I need ur help!
The search engine's results should be displayed on the same page as the engine. (preferably in a table) I have been trying to play with various MySql scripts I got from tutorials and w3schools.com but I can't figure this out:
How do I write the piece of code that makes my search.php select from my DB_table what is being searched for in the search engine?
Been trying this last time using mysql :
<form action='./search.php' method='get'>
<input type='text' name='k' size='50' value='<?php echo $_GET['k']; ?>' />
<input type='submit' value='Search' />
</form>
<hr />
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM Callflow WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 1)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
<?php
$db = new PDO('mysql:host=localhost;dbname=voizxl_wachtrij;charset=utf8', 'root', '');
?>
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
while ($row = mysql_fetch_assoc($query)){
$id = $row['calliipid'];
$title = $row['calleridname'];
$keywords = $row['calleridnum'];
echo "<h2><a href='$title'</a></h2>
$keywords<br /><br />";
}
}
else
echo "No results found for \<b>$k</b>\"";
mysql_close();
?>
Only when I tried this code I got errors, but I post it so u can see what i'm trying to achieve.
Now in PDO I can't figure out how to write this.. I'm experimenting with codes like :
<?php
$db = new PDO('mysql:host=localhost;dbname=voizxl_wachtrij;charset=utf8', 'root', '');
?>
<?php
foreach($db->query('SELECT * FROM Callflow') as $row) {
echo $row['calleridname'];
}
?>
<?php
$stmt = $db->prepare("SELECT * FROM Callflow WHERE id=:id AND name=:name");
$stmt->execute(array(':name' => $name, ':id' => $id));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<?php
$stmt = $db->query('SELECT * FROM table');
$row_count = $stmt->rowCount();
echo $row_count.' rows selected';
?>
Could someone please help by explaining the logic in writing the code or by giving an example of how to achieve what I want? Would be very much appreciated! TY in advanced!