List to URL Parameter - php

I have a bunch of integers which act as ID's within an ArrayList in my Android application.
This list of id's can be of any amount ranging from 0 - x. These id's are to identify players in a match. What I want is to send data to the web service, telling it that players 1,2 and 3 are on team 1 and players 4,5 and 6 are on team 2.
How best can I construct the URL with the above and how do I properly "expect" and "intercept" these parameters on the server side? I am using PHP.
So far I can think of the following:
$p1 = $_GET['p1'];
$p2 = $_GET['p2'];
$p3 = $_GET['p3'];
But as you can probably guess, that's really bad as I don't know how many people will be in team 1 and how many people in team 2. And further...how to seperate them.
Any ideas?

You can do a simple list of players in each teams :
// $_GET['team1'] = '1,2,3';
$team1 = $_GET['team1'];
$team1 = explode(',', $team1);
// $_GET['team2'] = '4,5,6';
$team2 = $_GET['team2'];
$team2 = explode(',', $team2);
Then you have an array per team, containing the players id.
Hope it helped !
EDIT:
Care of query maxlength here is a link :
Please note that PHP setups with the suhosin patch installed will have
a default limit of 512 characters for get parameters. Although bad
practice, most browsers (including IE) supports URLs up to around 2000
characters, while Apache has a default of 8000.
To add support for long parameters with suhosin, add
suhosin.get.max_value_length = in php.ini

Related

Puzzling superset in SQL

I'm querying a database (SQL) to find systems sending requests to see which are various different versions of windows (and other OSs). I check for a few known flavours and then try to get a catch-all but the catch-all catches fewer than the individual terms and I can't work out why.
e.g. for this PHP/SQL:
$monthSelect = "SELECT DISTINCT(host) FROM $table WHERE date > ('$lastMonth') ";
$v61 = $db->query($monthSelect."AND sys LIKE '%win32_v6.1%'")->num_rows;
echo "v61: $v61<br>\n";
$v62 = $db->query($monthSelect."AND sys LIKE '%win32_v6.2%'")->num_rows;
echo "v62: $v62<br>\n";
$nWin = $db->query($monthSelect."AND sys LIKE '%win32_v%'")->num_rows;
echo "nWin: $nWin<br>\n";
I get the following output:
v61: 1907
v62: 2181
nWin: 4036
Where 4036 is obviously less than 2181+1907. But shouldn't $nWin be providing a superset of the $v62, $v61 and any other versions that are being found?
I'm at a loss as to how I could be getting these answers
You're doing a SELECT DISTINCT.
If there are duplicate hosts in both of the first 2 queries, they will only be counted once in the third query. So the total of the third query would be less than the sum of the first two.

PDO using MySql Case for ordering with bound variables

I've been researching this and I think I've found the best solution but I'm not 100% sure. I have a good handle on PDO but this is the first time I've encountered CASE in mysql. This code is working but I'm wondering if it's efficient? If I were to have multiple keys, I would have to write many arrays to be able to search and order. Is there a shorter way to write this code or is this the most efficient? Thanks!
$filters = "
AND (name LIKE :keys
OR note LIKE :keys
OR tagnum = :skeys)
";
$order = "
ORDER BY
CASE
WHEN tagnum = :skeys THEN 0
WHEN name = :skeys THEN 1
WHEN name LIKE :lkeys THEN 2
WHEN name LIKE :rkeys THEN 3
ELSE 4
END
ASC
";
$arr[':keys'] = "%$keys%"; // both wild cards
$arr[':skeys'] = $keys; // stripped keys, no wild cards
$arr[':lkeys'] = "$keys%"; // left key, right wild card
$arr[':rkeys'] = "%$keys"; // right key, left wild card
If you want to have complete control over how you order your resultset, then your solution is perfect. However, if you have multiple keys, then I would definitely consider using fulltext search because it will be faster and lot less comlicated to code. However, it has a different ranking algorithm than you have now.

How can i UPDATE a sql field with a variable instead of a field name?

I'm new here (and not english guy, obviously), but I have a problem.
I have a SQL request, it's an UPDATE like the following :
$rep = $bdd->exec("UPDATE z_agenda SET AGENDA_1='$code'WHERE AGENDA_NOM='$agent' AND AGENDA_TYPE='code'");
BUT, and now the fun is incoming, I want to change AGENDA_1 to a variable which can contains AGENDA_1, AGENDA_2, etc. until AGENDA_31.
But it seems SQL doesn't like it.
So, anybody has an idea?
I'm completely stuck right now.
If you want more explanations, I'm here.
Sit, wait, and read some help forum
I'm adding some few code :
"
$mois = $_POST['mois']; (integer)
$debut = $_POST['debut']; (integer : 1-31)
$lettre = $_POST['lettre']; (integer)
$couleur = $_POST['couleur']; (integer)
$agent = $_POST['agent']; (string)
$code = $lettre + $couleur;
$rep = $bdd->exec("UPDATE z_agenda
SET AGENDA_1='$code'
WHERE AGENDA_NOM='$agent'
AND AGENDA_TYPE='code'");
"
my database contain few information columns, and 31 columns for each day. One line/month/user
don't know how manage my database with an other solution.
There's actually quite a lot going on here.
you should consider using prepared statements to prevent SQL injection vulnerabilities;
you should read up on database normalization;
you could expand the string and add the columns dynamically using, for example, a for loop but you don't want to do this!
Having numbered columns is usually a Very Bad Idea. Click the database normalization link for detailed information and thorough guidelines on how to proceed. Your application will get unmaintainable with a database structure like this. You'll be writing 'string building loops' for the rest of your life, whereas problems like the one you're having now have been solved a million times before.
The loop is static just for an example but you can set the inner code into your accordingly
for($i=0;$i<=31;$i++)
{
$agenda_coloumn = 'AGENDA_'.$i;
$rep = $bdd->exec("UPDATE z_agenda SET $agenda_coloumn = '$code' WHERE AGENDA_NOM='$agent' AND AGENDA_TYPE='code'");
}

JSON from SQL Table get double result

first of all i have to tell you that it is my first step on php and JSON.
I decided to use JSON to get value from a customer SQL Table.
I get my results using this script :
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
#mysql_select_db($config['db_name']) or die( "Unable to select database");
mysql_query('SET CHARACTER SET utf8');
$fet=mysql_query('select * from vehicule');
$json = array();
while($r=mysql_fetch_array($fet)){
$json[] = $r;
}
header('Content-Type: application/json');
echo $json_data=json_encode($json);
Everything is ok, exept that my JSON results looks like :
0 = 462;
1 = "Hyundai ix20 crdi 115 panoramic sunsation";
10 = 1346450400;
11 = "462-Hyundai-ix20-crdi-115-panoramic-sunsation";
12 = 462;
...
id = 462;
kilometrage = 14400;
marque = 4;
modele = 137;
motorisation = 2;
ordre = 462;
prix = 17500;
puissance = 6;
titre = "Hyundai ix20 crdi 115 panoramic sunsation";
url = "462-Hyundai-ix20-crdi-115-panoramic-sunsation";
...
I have result of the table in 2 versions : one with 0:value, 1:value, 2... and the other one using the table key, how can i print only the second one ?
By the way can someone give me link so i can know by what i have to replace mysql which is think out of date ? (i'm a beginner few hours using PHP)
Thank you very much !
You have two different issues happening here. One is outright causing the issue you are seeing, and the other is a bad practice mistake that will leave you wide open for trouble in the long run.
The first issue is the one you're asking about. The mysql_fetch_array function (see the Docs here) expects a minimum of one input (the result input) that you are providing. It also has a second, optional input. That optional input defaults to MYSQL_BOTH, which returns an associative array with the results available both through keys (column names) and their indexes. Which is to say, that if you select the column 'id', you get it's value in both $array[0] and $array['id']. It's duplicated, and thus the JSON process carries over the duplication. You need to provide a second value to the function, either MYSQL_ASSOC to get $array['id'] or MYSQL_NUM to get $array[0].
Your second issue is the choice of functions. You're using the 'raw' mysql functions. These have been depreciated, which is a technical term that means 'these functions are no longer supported, but we've left them in to give you time to fix legacy code'. For legacy, read 'old'. Those functions will be going away soon, and you need to upgrade to a better option -- either the mysqli functions, or the PDO class. I strongly recommend the PDO class, as once you learn it it's easy to learn and has the advantage of being more portable. Whichever set you go with, you need to learn to use prepared statements as both a performance and security issue. Right at the moment, you're working with 'raw' statements which have a history of being very easy to interfere with via what's called an 'injection attack'. You can see a fictionalized example of such an attack here, and there are plenty of articles online about it. These attacks can be incredibly complex and difficult to fight, so using prepared statements (which handle it for you), is strongly recommended. In the specific example you're using here, you don't need to worry about it because you aren't including any user inputs, but it's an important habit to get into.

PHP question on while query

Could someone help me figure out how to do this.
I have a game where a player can use potions to enhance their abilities.
This is on a timer which works fine. However Im now looking to add to this query when the player uses 2 different type of potions. I can get it to work but not 100% how I wish.
Player uses potion 1 and their stats are boosted by 20% for 20minutes.
Player then uses potion 2 and their stats are boosted by 60% for 20minutes.
With my code at the moment
$check = sprintf("SELECT time,strmod FROM `effects` WHERE `userid` = %u", $userid);
$exe = mysql_query($check);
while($bonus = mysql_fetch_array($exe))
{
$last = $bonus['time'];
$strmod=$bonus['strmod']);
It will display the users bonus (base strength in this example is 2,364,195)
Potion 1 boost your base strength to 2837034 (+20%)
Potion 2 boost your base strength to 3782712 (+60%)
Is there a solution where I can get the second 60% potion to take into account the bonus received from the 1st potion. So 60% of 2837034 rather than 2364195. Before I consider recoding the whole thing :D
I hope thats clear bit hard to explain.
Thanks
I presume the database table contains the list of potions in effect. Try something which keeps the value over the while loop...
$multiplier = 1;
while($bonus = mysql_fetch_array($exe))
{
$last = $bonus['time'];
$strmod=$bonus['strmod']);
$multiplier += $strmod/100; // convert from percent to decimal
}
$currStrength = $multiplier*$baseStrength;
Probably not the way your doing it, since your not recording the original players HP, you just have the current (after modifiers have been applied) HP.

Categories