Simple Multiple Tables Search in PHP PDO
This is a Simple Multiple Tables Search in PHP PDO .
- First Create A Database called "multitable" in phpmyadmin.
- After Creating Database in phpmyadmin create table called 'searchkeyword','posts' .
- Add Column's on searchkeyword as sid and set as autoincrement ,searchkeyword and set as varchar.
- Then Add Column's on posts as post_id and set as autoincrement ,post_title,post_content and set as varchar, and sid as integer.
- And last of database section insert some values on table columns and keep sid of searchkeyword table=posts table.
Now Lets Start For Codding For Multiple Table Search in PHP PDO
- First You need to connect with Datebase 'Connection in PDO'.
- <?php
- $host = "localhost";
- $user = "root";
- $password = "";
- $database_name = "multitable";
- $dbcon = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
- ));
- ?>
- Now Create Simple HTML form As This Below;
- <form action="index.php" method="post">
- <input type="text" placeholder="Search" name="searchkeyword">
- <input type="submit" name="submit" value="submit">
- </form>
- AT Last This Scripts is for multiple table search process in pdo
- <?php
- if(isset($_POST['submit'])){
- ?>
- <?php
- $keyword=$_POST['searchkeyword'];
- // Query For Multiple Tables Search ( LIMIT 0 , 10) This Limit shows upto Ten records
- $query = $dbcon->prepare("select c.* , p.* from searchkeyword c,posts p where searchkeyword LIKE '%$keyword%' AND c.sid= p.sid LIMIT 0 , 10");
- $query->bindValue(1, "%$keyword%", PDO::PARAM_STR);
- $query->execute();
- $strong_keyword = '<strong>'.$keyword.'</strong>';
- // Display search result Here
- if (!$query->rowCount() == 0) {
- echo " Search found :<br/>";
- while ($results = $query->fetch()) {
- ?>
- <?php echo '<h1>'.str_ireplace($keyword,$strong_keyword,$results['post_title']).'</h1>'; ?><br />
- <?php echo '<h3>'.str_ireplace($keyword,$strong_keyword,$results['post_content']).'</h3>'; ?>
- <?php }
- } else {?>
- <?php echo 'Nothing found'; ?>
- <?php }
- ?>
- <?php
- }
- ?>
Note: Save All This Scripts in index.php file..
No comments:
Post a Comment