Jump to content

problem with data_seek


heraldic2

Recommended Posts

I am doing a website for my fantasy football league.  We are putting up a record book which will be a database driven page written in php.  What I would like to do is to query the database and if there is information display it.  If there is no information then I want it to say Never achieved.

 

Here is the code:

 

$con = mysql_connect($hostname, $username, $password)

  OR DIE ('Unable to connect to database! Please try again later.');

  $db = mysql_select_db($dbname, $con);

 

$query = "SELECT teamname, division_records.division, div_win, div_loss FROM division_records, owners WHERE owners.owner_id = division_records.owner_id AND division_records.year = 2011 ORDER BY division_records.division";

$result = mysql_query($query);

$row = mysql_fetch_array($result);

 

if (!$result) {

    echo 'Never achieved';

    exit;

}

 

echo '<ol>';

mysql_data_seek($result, 0);

$row = mysql_fetch_array($result);

while ($row = mysql_fetch_array($result)) {

echo '<li><b>' . $row['division'] . '</b>: ' . $row['teamname'] . '<br/>';

echo 'Divisional Record: ' . $row['div_win'] . '-' . $row['div_loss'] . '</li>';

}

echo '</ol>';

mysql_close($con);

 

 

The problem is if the query returns nothing then mysql_data_seek comes back with an error, if I dont include mysql_data_seek then the query displays on the second record, and it never says "Never achieved"

 

Any pointers would be greatly helpful.

Link to comment
https://forums.phpfreaks.com/topic/235889-problem-with-data_seek/
Share on other sites

alright try this

$con = mysql_connect($hostname, $username, $password)
   OR DIE ('Unable to connect to database! Please try again later.');
   $db = mysql_select_db($dbname, $con);

$query = "SELECT teamname, division_records.division, div_win, div_loss FROM division_records, owners WHERE owners.owner_id = division_records.owner_id AND division_records.year = 2011 ORDER BY division_records.division";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);

if ($num_rows == 0) {
    echo 'Never achieved';
    exit;
}

echo '<ol>';
while ($row = mysql_fetch_array($result)) {
echo '<li><b>' . $row['division'] . '</b>: ' . $row['teamname'] . '<br/>';
echo 'Divisional Record: ' . $row['div_win'] . '-' . $row['div_loss'] . '</li>';
}
echo '</ol>';
mysql_close($con);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.