This is a method for displaying or fetching data from multiple mysql tables using php pdo in a bootstrap table.
- First Create A Database called "multitable" in phpmyadmin.
- After Creating Database in phpmyadmin create tables called 'makes' and 'country' .
- Then Add Column's on country as country_id and set as autoincrement ,country_name,love and set as varchar.
- Then Add Column's on makes as makes_id and set as autoincrement ,makes_name,love and set as varchar and set country_id as int.
- And last of database section insert some values on tables columns of country,makes table and insert country_id of table makes as equal to country_id of country table.
Now Lets Start displaying or fetching data from multiple mysql tables using php pdo.
Lets create new file and save it as index.php and write some php pdo scripts in a php file index.php for database connection now which connect to your database multitable.
- <?Php
- // pdo database connection
- $dbhost = 'localhost';
- $dbname = 'multitable';
- $dbuser = 'root';
- $dbpass = '';
- try{
- $dbcon = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
- $dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }catch(PDOException $ex){
- die($ex->getMessage());
- }
- ?>
Now write some php pdo scripts for fetching records from a multiple mysql tables in a index.php file again.
- <?php
-
- //query for selecting database table and displays records from multiple tables
- $stmt = $dbcon->prepare('select c.* , p.* from country c,makes p where c.country_id= p.country_id LIMIT 0 , 10');
- $stmt->execute();
- // this while statement displays rows of database table
- while($row=$stmt->fetch(PDO::FETCH_ASSOC))
- {
- extract($row);
- ?>
- <?php echo $row['country_name']; ?>
- <?php echo $row['makes_name']; ?>
- <?php echo $row['love']; ?>
- </tr>
- <?php
- }
- ?>
Full scripts in a file index.php
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Displaying or Fetching records from multiple mysql tables using php pdo in a bootstrap table</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
- <script src="../js/jquery.min.js"></script>
- <script src="../bootstrap/bootstrap.min.js"></script>
-
- </head>
- <body>
- <div class="container">
- <?Php
- // pdo database connection
- date_default_timezone_set("Asia/Kathmandu");
- $dbhost = 'localhost';
- $dbname = 'multitable';
- $dbuser = 'root';
- $dbpass = '';
-
-
- try{
-
- $dbcon = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass);
- $dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
-
- }catch(PDOException $ex){
-
- die($ex->getMessage());
- }
-
- ?>
- <h3>Displaying or Fetching records from multiple mysql tables using php pdo in a bootstrap table</h3>
-
- <table class="table table-striped">
- <thead>
- <tr class="danger">
- <th>Countryname</th>
- <th>Makes name</th>
- <th>love</th>
- </tr>
- </thead>
- <tbody>
- <?php
-
- //query for selecting database table
- $stmt = $dbcon->prepare('select c.* , p.* from country c,makes p where c.country_id= p.country_id LIMIT 0 , 10');
- $stmt->execute();
- // this while statement displays rows of database table
- while($row=$stmt->fetch(PDO::FETCH_ASSOC))
- {
- extract($row);
- ?>
- <tr class="info">
- <td><?php echo $row['country_name']; ?></td>
- <td> <?php echo $row['makes_name']; ?></td>
- <td><?php echo $row['love']; ?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- </div>
-
- </body>
- </html>
No comments:
Post a Comment