How to get the next row from the Database table? Such that the row might be incremented row that is if structure has a field called “id” then the row can be id++ or very next incremented row, but it may also b any other id containing row (NOT VERY NEXT) , (because the ids could be deleted from the table).
I am using Mysql Database
below is my code..
mysql_select_db('pranav_test');
$seletaudit = mysql_query("SELECT * FROM jos_audittrail WHERE live = 0");
while($row2 = mysql_fetch_array($seletaudit))
{
$audit[] =$row2;
}
$trackid = array();
$tracktable = array();
$i = 0;
foreach($audit as $val)
{
$trackid[$i] = $val['trackid'];
$tracktable[$i] = $val['table_name'];
if($i!=0)
{
$prev_id = $trackid[$i-1];
$prev_valtab = $tracktable[$i-1];
}
if($val['operation'] == 'INSERT')
{
if($tracktable[$i]!=$prev_valtab)
{
$inserttable = "INSERT INTO '".$tracktable[$i]."' (";
}
if($tracktable[$i]==$prev_valtab)
{
$insertfield .= "'".$val['field']."', ";
}
if($tracktable[$i]==$prev_valtab)
{
$insertfield .= "]";
$insertfield = str_replace(", ]",")",$insertfield);
}
}
}
here above I need t know whats going to be my next rows’s “table_name” field contain…
how can i do that….?
Please help for fetching the next row in the for loop to get it checked.
UPDATE:
Ok, now that you posted some code, here a more specific answer:
If you want to have access to the next row in the array, I suggest to use a normal
forloop to have access to the index:But be careful when you reach the end of the array, as
$i+1will be out of bounds.Or if you just wonder why the
$iin your current code is not behaving correctly:You have to increment it at the end of your
foreachloop:UPDATE2:
A more complete example to make it clear (I adapt your approach and check for the previous row)
As you have not specified at all how you connect to the database, here is a generic answer:
Specify your query to order the result set by ID:
Then get the result set (we have a fictive database class):
Then iterate over the resultset: