Jump to content

[SOLVED] LEFT JOIN


MikeDXUNL

Recommended Posts

TABLE `achievements`

achid | gameid |    achname      |

1            5        Perfectionist

2            5              5x Eggs

 

TABLE `guides`

guideid | achid | gameid | guidetext

1              1          5          Complete the game, etc etc

2              2          5          Throw 5 Eggs

 

 

<?php
$gameid = $_GET['gameid']; //in this case it is 5

$get_achs = mysql_query("SELECT * FROM achievements LEFT JOIN guides on achievements.achid = guide.achid WHERE achievements.gameid = '$gameid'") or die(mysql_error());

while($ach = mysql_fetch_array($get_achs, MYSQL_ASSOC)) {
echo $ach['guidetext'];
}

?>

 

instead of getting my result, i get:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\achunlocked\viewgame.php on line 196

 

Any Help please? :)

Thanks in advance,

Mike

 

Link to comment
https://forums.phpfreaks.com/topic/112692-solved-left-join/
Share on other sites

This might not help but I added a typecast statement to remove the SQL injection risk. Give it a try and tell us if it works. The error you're getting means that the value in $get_achs is not a valid MySQL result set.

 

<?php

$gameid = (int)$_GET['gameid']; // Typecast as int. Remove SQL injection risk

$get_achs = mysql_query("SELECT * FROM achievements LEFT JOIN guides on achievements.achid = guide.achid WHERE achievements.gameid = '$gameid'") or die(mysql_error());

if (mysql_num_rows($get_achs) > 0)
{
while($ach = mysql_fetch_assoc($get_achs)) {
echo $ach['guidetext'];
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/112692-solved-left-join/#findComment-578758
Share on other sites

Congratulations! But please, still take my advise about typecasting.

 

<?php

$gameid = (int)$_GET['gameid']; 

?>

 

By doing this it forces the variable $gameid to contain a number so if a user entered malicious code into this variable in the URL, it would be automatically stripped out. Without this your code is wide open to SQL injection.

 

Please hit the 'Topic Solved' link in the bottom left of this page, if that is all?  :)

Link to comment
https://forums.phpfreaks.com/topic/112692-solved-left-join/#findComment-578768
Share on other sites

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.