skip missing rows
Posted: Wed Feb 23, 2005 12:08 pm
I have a mysql db that has a table of links i wrote a script that pulls the links in order by id number. The id numbers are like 1,2,3,4... however i want to make it so that if i delete a row from the database ane make it like 1,3,4 it wont error out when it reaches row 2. How could i go about making something that will check to see if the row is there if not move to the next and so forth?
Here is the code im working with now to give you an idea
Here is the code im working with now to give you an idea
Code: Select all
<?
include_once ("config.php");
//Find current position in rotator
$result = mysql_query("SELECT * FROM position WHERE num = '1' ");
while($myrow = mysql_fetch_assoc($result))
{
//now print the results:
$position= $myrowї'position'];
$total_count= $myrowї'count'];
$total_links= $myrowї'total_links'];
}
//determine wether or not to start rotator over
if ($position > $total_links){
$query = "UPDATE `position` SET `position` = '1' WHERE `num` = '1' ";
$result = mysql_query($query) or die("error: " . mysql_error());
$position = "1";
} else {
};
//pull link from database
$result = mysql_query("SELECT * FROM links WHERE id = "$position" ");
while($myrow = mysql_fetch_assoc($result))
{
//now print the results:
$id= $myrowї'id'];
$url= $myrowї'url'];
$count= $myrowї'count'];
}
// record hit to url
$new_hit = $count+1;
$query = "UPDATE `links` SET `count` = $new_hit WHERE id = "$position" ";
$result = mysql_query($query) or die ("error: " . mysql_error());
// record hit to total hits in position table
$new_total = $total_count+1;
$query = "UPDATE `position` SET `count` = $new_total ";
$result = mysql_query($query) or die ("error: " . mysql_error());
// add to position number so that it will rotate to the next url
$new_position = $position+1;
$query = "UPDATE `position` SET `position` = $new_position ";
$result = mysql_query($query) or die ("error: " . mysql_error());
// Display url
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=<? echo $url; ?>">
</head>
</html>