Content
- How to create automatic counter of record number in MySQL table using PHP?
- How to delete all files in a folder?
- Is it possible in PHP to replace blocks if…else with another conditional operation?
- How to delete strings from a text file using the PHP language?
How to create automatic counter of record number in MySQL table using PHP?
For this, we’ll use the following function:
<?php
// The function is designed for obtainment of record number of the table
function get_id($tbl_dt)
{ // Do descending sorting
$result = mysql_query("SELECT id FROM ".$tbl_dt." ORDER BY `id` DESC");
$myrow = mysql_fetch_array($result);
$num_rows = mysql_num_rows($result);
// if a number of records coincide with the last id, then...
if ($num_rows==$myrow['id'])
{ // ...take the first element
//in the sampling with the highest id value ,
// by increasing this value by a unity
return $myrow['id'] + 1;
} else { // Do ascending sorting, then...
$res1=mysql_query("SELECT id FROM ".$tbl_dt." ORDER BY `id` ASC");
$myrow1 = mysql_fetch_array($res1);
$i = 1;
do // ...look for "empty" id and add a record for this id
{
if ($i == $myrow1['id']) {
$i++;
continue;
} else {
return $i;
}
}
while ($myrow1 = mysql_fetch_array($res1));
}
}
?>


Recent Comments