I have this code which takes data from an sqlite3 database and prints out the all the data from the data into an html table. Like so:

1 jake asdlfj lakdsjfl lkajsdgklj kafdlgjkalg 2011-12-04
2 Bill asdfohdf oijasdofij qoasjgfoiq oaisjdfae 2011-12-04
3 Tim lakdsfgaie aogoie ;lasjdlo asdfgae 2011-12-04
4 hello adfase wqer qwtwt wqerwqer 2011-12-05

The data in the table is just random typing to see if it works. Anyways what I want to do is to only print out one row of the table to be printed out. I want to be able to call the function and grab the row i need and print out only that row on my webpage. I can't seem to find a php function to do this. Only one's that take an array of the rows and prints out the entire thing. Can someone help. My code is below.

<?php
function display()
{
        $db = new SQLite3('jobs.db');

        $sql = "SELECT * FROM jobs";

        $result = $db->query($sql);//->fetchArray(SQLITE3_ASSOC);


         while($res = $result->fetchArray(SQLITE3_ASSOC)){
             echo "<table border=1>";
             echo "<tr><td>";
             echo $res['id'];
             echo "</td><td>";
             echo $res['jobtitle'];
             echo "</td><td>";
             echo $res['employer'];
             echo "</td><td>";
             echo $res['location'];
             echo "</td><td>";
             echo $res['email'];
             echo "</td><td>";
             echo $res['description'];
             echo "</td><td>";
             echo $res['date'];
             echo "</td></tr>";
             echo "</table>";


          }
}

?>

If you want to display one row you need to either change your query to include a WHERE clause rather than selecting all of the records, or select all of the records and then use and IF statement to determine which row you want to display.

Using the ID as example:

$sql = "SELECT * FROM jobs WHERE id='".$id."'";

or

while($res = $result->fetchArray(SQLITE3_ASSOC)){
if ($res['id'] == $id) {
             echo "<table border=1>";
             echo "<tr><td>";
             echo $res['id'];
             echo "</td><td>";
             echo $res['jobtitle'];
             echo "</td><td>";
             echo $res['employer'];
             echo "</td><td>";
             echo $res['location'];
             echo "</td><td>";
             echo $res['email'];
             echo "</td><td>";
             echo $res['description'];
             echo "</td><td>";
             echo $res['date'];
             echo "</td></tr>";
             echo "</table>";
 }
 
          }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.