0

I have a table as below,

 ID     Name       Age
 ----------------------
  100       A         10
  203       B         20

Now how do i select only row1 using MySQL SELECT command and then I've to increase +1 to it to select row2. In short I'll be using for loop to do certain operations.

Thanks.

2
  • 1
    This is covered in every basic "PHP and databases" tutorial, is it not? Commented Sep 11, 2011 at 16:19
  • Will mysql_fetch_row help me ? Commented Sep 11, 2011 at 16:22

5 Answers 5

1

Sounds like you've got a mix up. You want to select all the rows you want to iterate through in your for loop with your query, and then iterate through them one by one using php's mysql functions like mysql_fetch_row

Sign up to request clarification or add additional context in comments.

Comments

0

You should not try to use tables in a linear fashion like this. Set your criteria, sorting as appropriate, and then to select the next row use your existing criteria and limit it to one row.

SELECT * FROM `table` ORDER BY `ID` LIMIT 1
SELECT * FROM `table` ORDER BY `ID` WHERE ID > 100 LIMIT 1

Comments

0

You'd probably be better off retrieving all rows that you need, then using this. Note the LIMIT is entirely optional.

$query = mysql_query(' SELECT ID, Name, Age FROM table_name WHERE condition LIMIT max_number_you_want '))
while ($row = mysql_fetch_assoc($query)
{
    // Do stuff
    // $row['ID'], $row['Name'], $row['Age']
}

Lots of small queries to the database will execute much slower than one decent-sized one.

Comments

0

You should get the result into an array (php.net : mysql_fetch_*). And after you'll can loop on the array "to do certain operations"

Comments

0

Yep, this is a pretty common thing to do in PHP. Like the others who have posted, here is my version (using objects instead of arrays):

$result = mysql_query("SELECT * FROM table_name");
while ($row = mysql_fetch_object($result)) {
  // Results are now in the $row variable.
  // ex:  $row->ID, $row->Name, $row->Age
}

Comments

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.