0

Here is what i am trying to do

{
{
    $sql="SELECT ISO_id,gdp,population,country_name,gold,silver,bronze,total FROM Country ORDER BY (46034/($_GET["x"]*bronze)+($_GET["y]*silver]+(z*gold)*(gdp/population))"; // chosen to order my html table with the number the medals each country won at the olympics
    }
    }

however i do not know how to include the $_GET inside the statements

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
user2359244
  • 37
  • 1
  • 6

4 Answers4

0

To use a $_GET parameter in your current statement, use:

$sql="
    SELECT ISO_id,gdp,population,country_name,gold,silver,bronze,total 
    FROM Country
    ORDER BY (46034/(" . $_GET["x"] . "*bronze)+(" . $_GET["y"] . "*silver]+(z*gold)*(gdp/population))";

However, please be aware that this leaves you open to SQL Injection attacks, so you should consider filtering your input before using it in such a query. For details, see How to prevent SQL injection in PHP?

Also, please note that there appears to be an error in your ORDER BY clause as stated by Nagasaki in the comments. You should order on a column name.

Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
0
$sql="SELECT 
    ISO_id,
    gdp,
    population,
    country_name,
    gold,
    silver,
    bronze,
    total,
    (46034/(" . $_GET["x"] . "*bronze)+(" . $_GET["y"] . "*silver]+(z*gold)*(gdp/population)) as number_medals
FROM Country
ORDER BY number_medals";

Be careful of sql injection....

Nagasaki
  • 60
  • 2
  • 16
0

its a good idea to first make the get variable into a separate variable then from there clean it up because putting get variables straight into an sql statement leaves you open to sql injection.

Say the url looks like this. http://www.mysite.com?ID=4 to get at the id you would go $_GET[ID] or to be safe $id = $_GET[ID] and from here do some work to clean any bad data from the id. http://php.net/manual/en/function.mysql-real-escape-string.php this will help you out a bit but still its never 100% save.

I remember back when i was trying to figure this out and i didnt want to look at alternatives i just wanted to make it work but a word of advice would be to use PDO prepared statements. They are a little tricky to understand at first but this guide is pretty good and will explain how to do it quite easily. PDO keeps you safe from sql injection.

PDO Prepared Statement Tutorial

Dan Hastings
  • 3,241
  • 7
  • 34
  • 71
-1

just add Braces

{ { $sql="SELECT ISO_id,gdp,population,country_name,gold,silver,bronze,total FROM Country ORDER BY (46034/({$_GET["x"]}*bronze)+({$_GET["y]}*silver]+(z*gold)*(gdp/population))"; // chosen to order my html table with the number the medals each country won at the olympics } }