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
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);

Link to comment
Share on other sites

sweet, what you were doing by have (!result) was telling it that if the query failed, to echo your 'Never achieved'. But even if the query returned 0 rows it still wasnt failing, so you would never see your empty query message..glad i could help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.