-2

i am trying to put data of each each from my database to each page by modifying it on each page

for example i have on my database with table of :

id fname lname age 1 bob carl 23 2 michael jake 25

now i want each line to show up on each page i want

for example i have page1.php this must out put the first line like this bob carl 23

and i have page2.php this must out put the second line like this michael jake 25

and so on.

i have a code like this

$sql = "SELECT * FROM pages";
$results = mysqli_query($connection,$sql);

if (mysqli_num_rows($results) > 0) {
    while ($row = mysqli_fetch_array($results)) {
        $id = $row["id"];
        $pname = $row["pname"];
        $pimg = $row["pimg"];
        $pstream = $row["pstream"];
    }
}

if i echo this out it is gonna output the last line only or with while loop it's gonna output all lines on one page. how can i mark from my code which line should be showing ?

2 Answers2

0

In order to show specific data on each page you must modify the query for each page by using additional clauses to filter the data. It all starts with a WHERE clause:

$sql = "SELECT * FROM pages WHERE id = 1";

The above query will get the data for the matching id. If you choose to perform dynamic changes to the filtering variables, please learn about prepared statements for MySQLi. Even escaping the string is not safe!

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
-1

In page1.php use:

$id=1;
$sql = "SELECT * FROM pages where id=$id";
$results = mysqli_query($connection,$sql);

if (mysqli_num_rows($results) > 0)
 {
        $row = mysqli_fetch_array($results);
        $id = $row["id"];
        $pname = $row["pname"];
        $pimg = $row["pimg"];
        $pstream = $row["pstream"];

}

and for page2.php set id to 2 and so on if you insist to use your method then try

 $sql = "SELECT * FROM pages ORDER BY id ASC";
    $results = mysqli_query($connection,$sql);
    $idToShow=1;
    $i=1;
    if (mysqli_num_rows($results) > 0) 
   {
        while ($row = mysqli_fetch_array($results))
       {
           if($id==$idToShow)
         {
            $id = $row["id"];
            $pname = $row["pname"];
            $pimg = $row["pimg"];
            $pstream = $row["pstream"];
         }
         $i++;
       }
    }

Like previous make $idToShow=2 for page 2nd and so on

Sujan Poudel
  • 794
  • 5
  • 16
  • Let's not teach/propagate sloppy and dangerous coding practices. If you post an answer without prepared statements [you may want to consider this before posting](http://meta.stackoverflow.com/q/344703/). Additionally [a more valuable answer comes from showing the OP the right method](https://meta.stackoverflow.com/a/290789/1011527). – Jay Blanchard May 30 '17 at 15:30
  • thanks a lot for your answer and your time writing this. but @Jay has answerd this. thanks again Suzan – wesley --------------------- May 30 '17 at 15:32
  • I'm sorry for mistake,it's my fault – Sujan Poudel May 30 '17 at 16:19