bcfantasy Posted March 21, 2007 Share Posted March 21, 2007 I'm having a minor problem with the following code: $data_query = "SELECT points FROM game_results WHERE player_id = '$player_id' AND game_id = '$game_id'"; $get_data = mysql_query($data_query); if ($data = mysql_result($get_data, 0)) { // do nothing because there was data } else { $data = "No Points" } If there is no entry in the game_results table for the selected player and game, I get an error - Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 4 in... . The code still works fine and displays "No Points" as expected, but I'm sure I'm not supposed to be receiving errors. Anyone got some pointers on how to eliminate the error? Link to comment https://forums.phpfreaks.com/topic/43744-solved-proper-mysql-syntax/ Share on other sites More sharing options...
grimmier Posted March 21, 2007 Share Posted March 21, 2007 $data_query = "SELECT points FROM game_results WHERE player_id = '$player_id' AND game_id = '$game_id'"; $get_data = mysql_query($data_query); if ($data = mysql_result($get_data, 0) == false) { $data = "No Points" } just a shot in the dark here, but try this. Link to comment https://forums.phpfreaks.com/topic/43744-solved-proper-mysql-syntax/#findComment-212380 Share on other sites More sharing options...
bcfantasy Posted March 21, 2007 Author Share Posted March 21, 2007 Didn't work. I get the same error. Link to comment https://forums.phpfreaks.com/topic/43744-solved-proper-mysql-syntax/#findComment-212420 Share on other sites More sharing options...
redarrow Posted March 21, 2007 Share Posted March 21, 2007 <?php $data_query = "SELECT points FROM game_results WHERE player_id = '$player_id' AND game_id = '$game_id'"; $get_data = mysql_query($data_query); if (mysql_num_rows($points) == 0) { echo "No Points"; } ?> Link to comment https://forums.phpfreaks.com/topic/43744-solved-proper-mysql-syntax/#findComment-212424 Share on other sites More sharing options...
shaunrigby Posted March 21, 2007 Share Posted March 21, 2007 $data_query = "SELECT `points` FROM `game_results` WHERE `player_id` = '$player_id' AND `game_id` = '$game_id'"; $get_data = mysql_query($data_query); $data = mysql_fetch_array($get_data) if($data['points'] > 0){ $result = $data['points']; } else if ($data['points'] <= 0) $result = "No Points"; } else { $result = "An unknown error occurred!"; } Link to comment https://forums.phpfreaks.com/topic/43744-solved-proper-mysql-syntax/#findComment-212426 Share on other sites More sharing options...
redarrow Posted March 21, 2007 Share Posted March 21, 2007 I think you ment this example sorry. <?php $data_query = "SELECT * FROM game_results WHERE player_id = '$player_id' AND game_id = '$game_id'"; $result=mysql_query($data_query); while($rec=mysql_fetch_assoc($result){ if ($rec['points']== 0) { $data= "No Points"; }else{ $data="There Points"; } echo $data; ?> Link to comment https://forums.phpfreaks.com/topic/43744-solved-proper-mysql-syntax/#findComment-212428 Share on other sites More sharing options...
bcfantasy Posted March 22, 2007 Author Share Posted March 22, 2007 Thanks, guys. shaunrigby's solution worked. redarrow, your second solution sets the value of $data to false if there is none, so I'm guessing if I changed to for "== 0" to "== false" your solution would work as well. Link to comment https://forums.phpfreaks.com/topic/43744-solved-proper-mysql-syntax/#findComment-212459 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.