heraldic2 Posted May 9, 2011 Share Posted May 9, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235889-problem-with-data_seek/ Share on other sites More sharing options...
fugix Posted May 9, 2011 Share Posted May 9, 2011 what do you receving when you change your query line to this. $result = mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/235889-problem-with-data_seek/#findComment-1212610 Share on other sites More sharing options...
heraldic2 Posted May 9, 2011 Author Share Posted May 9, 2011 There is no change if I add: $result = mysql_query($query) or die(mysql_error()); to the php code. Thank you for trying Quote Link to comment https://forums.phpfreaks.com/topic/235889-problem-with-data_seek/#findComment-1212615 Share on other sites More sharing options...
fugix Posted May 9, 2011 Share Posted May 9, 2011 yeah, the mysql_seek_data() function will return with an E_WARNING if the result is null Quote Link to comment https://forums.phpfreaks.com/topic/235889-problem-with-data_seek/#findComment-1212620 Share on other sites More sharing options...
fugix Posted May 9, 2011 Share Posted May 9, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/235889-problem-with-data_seek/#findComment-1212622 Share on other sites More sharing options...
heraldic2 Posted May 9, 2011 Author Share Posted May 9, 2011 Works perfectly. So instead of: if (!result) use: if (num_rows==0) Thank you very much and I bow to your uberness Quote Link to comment https://forums.phpfreaks.com/topic/235889-problem-with-data_seek/#findComment-1212628 Share on other sites More sharing options...
fugix Posted May 9, 2011 Share Posted May 9, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235889-problem-with-data_seek/#findComment-1212633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.