-1

I have a simple my-sql table:

+----+----------+----------+-------------+-----------+
| id | Name    | Father    | National   | Value     |
+----+----------+----------+-------------+-----------+
|  1 | Daniel  | David    | American    |         0 |
|  2 | Rebbeka | Craig    | American    |         0 |
|  3 | Robert  | John     | American    |         0 |
|  4 | Arjun   | Daneil   | Indian      |         0 |
|  5 | Ana     | Peter    | British     |         0 |
+----+----------+----------+-------------+-----------+

I need to a php-script to query and fetch new single row every time it is executed. It may be based on ID.

About code/framework, i am using simple php5 with mysql on ubuntu server.

This is my code, the probelem is that it outputs whole of the Name,Father columns every time on call, i just want to print a single row everytime it is executed. and on every php script execution a new row should be printed based on id in ascending order.

<?php

$con = mysqli_connect('localhost','user','pass','database');

$result = mysqli_query($con,"select * from table");

while($row = mysqli_fetch_array($result)) {
        echo "Name:" . $row['name'] . " " . "Father's Name:" . $row['father'];
        echo "<br>";
}

mysqli_close($con);

?>

Every help is much appreciated.

5
  • 1
    Have you tryied anything? What database are you using? Any framework? Commented Jul 23, 2014 at 9:17
  • Atleast try something and post question .. Commented Jul 23, 2014 at 9:19
  • i think before you edit your question you should read How do I ask a good question? Commented Jul 23, 2014 at 9:24
  • :D You just updated it ... Now i have answered it according to the last edit ... :) hope you like it Commented Jul 23, 2014 at 9:52
  • i will need the 2 way please. Commented Jul 23, 2014 at 11:05

3 Answers 3

0

the below code will help you get the output. from next time when you ask any question pls add your code.

$link = mysqli_connect("localhost","root","","test") or die("Error " . mysqli_error($link));

$query = "SELECT * FROM `emp` ORDER BY RAND() LIMIT 0,1;" or die("Error in the consult.." . mysqli_error($link));

$result = mysqli_query($link, $query);

//display information:
$row = mysqli_fetch_array($result);

  echo $row["name"].$row["Father"].$row["National"].$row["Value"];
0

As i have no much details of what type of code/framework you have I am giving you a answer on the basis of what I understand

There will be two ways I can think of to fetch the data

1.

Change the Table Structure and add one more column called lastFetched and every-time you fetch the row update the particular row by 1 and else everything else to 0 so that next time when you fetch the row you already know which is the last row fetched by you. and you can do an increment to it.

2.

On the page you are fetching the result would have a hidden input where you can store the last data number letz say first time it is 0 when page loads

then your query will be

SELECT * from <tablename> LIMIT 0,1 

and increase the value of the hidden input to +1 so next time you fetch the result will gives the query

SELECT * from <tablename> LIMIT 1,2 
3
  • How can i do that in php5 please? Commented Jul 23, 2014 at 10:03
  • Which one would you like to go with 1 or 2 Commented Jul 23, 2014 at 10:30
  • I am adding a new answer for that Commented Jul 24, 2014 at 2:26
0

From the discussion we had in the comments I came to a conclusion that you are looking for this.

<table>
<tr>
<?php
if(isset($_POST['pullNextRecord'))
{
    $whtrec=$_POST['whatrecord'];
    $con = mysqli_connect('localhost','user','pass','database');
    $result = mysqli_query($con,"select * from table LIMIT $whtrec, ++$whtrec");
    echo "<input type='hidden' name='whatrecord' value=".$whtrec." />";
    echo "<th>Name</th><th>Fathers Name</th></tr><tr>";
    while($row = mysqli_fetch_array($result)) 
    {
        echo "<td>$row['name']</td><td>$row['father']</td>";
    }
    mysqli_close($con);
}
?>
<input type="submit" name="pullNextRecord" /> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.