By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

I need to get certain information out of a database

P: 2
Hello,

I am working on something and it's a database with a list of book titles and years they have been published, I need to get the books that have been written for 1880 from the database only and with my code it shows all of the books with all the years, please help.

My code:
Expand|Select|Wrap|Line Numbers
  1.         <?php
  2.  
  3.         $db = new PDO('mysql:host=localhost;dbname=boekenlijst','root','');
  4.  
  5.         $sql = "SELECT BoekNummer, Jaar, Titel FROM boeken";
  6.         $resultaat = $db->query($sql);
  7.         echo '<table border = 1>';
  8.         foreach($resultaat as $row) {
  9.         echo '<tr>';
  10.         $nummer = $row['BoekNummer'];    
  11.         echo '<td>'.$row['BoekNummer'].'</td>';
  12.         echo '<td>'.$row['Titel'].'</td>';
  13.         echo '<td>'.$row['Jaar'].'</td>';
  14.  
  15.         }
  16.         echo '</table>';
  17.  
  18.  
  19.  
  20.         $db= NULL;
  21.  
  22.         ?>
I am sorry that somethings are in an other language (dutch). Thanks in advance!

PS: jaar = year, BoekNummer =ID in the database and title = title
Dec 8 '12 #1
Share this Question
Share on Google+
1 Reply


PsychoCoder
Expert Mod 100+
P: 465
You're going to want to use a WHERE clause so you can limit what gets returned. So your query:
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT BoekNummer, Jaar, Titel FROM boeken";
Should turn to:
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT BoekNummer, Jaar, Titel FROM boeken WHERE jaar = '1880'";
Dec 8 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.