Jump to content

[SOLVED] proper mysql syntax


bcfantasy

Recommended Posts

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

$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.

 

$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!";
}

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

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.