I want the program to ignore zero totals. I'm trying to stop auto awards to non-participants with zero totals. I added the >= expression after the && operators. Is this correct for kills and networth?
$fetch_nor_killers = mysql_query("SELECT wk,bk,hk,dk,pk,mk, code, p, (wk+bk+hk+dk+pk+mk) as totalKills FROM r$round[round]_p WHERE status = '". normal ."' ORDER BY totalKills DESC LIMIT 3");
$killa_rank = 0;
while($killa = mysql_fetch_array($fetch_nor_killers))
{
$killa_rank++;
if($killa_rank == 1 && $killa['totalKills'] >= 1)
{
$killa_award = "freeopkiller1";
}
else
if($killa_rank == 2 && $killa['totalKills'] >= 1)
{
$killa_award = "freeopkiller2";
}
else
if($killa_rank == 3 && $killa['totalKills'] >= 1)
{
$killa_award = "freeopkiller3";
}
Same thing here with networth. Just trying to avoid awarding a inactivate user. #Jagrut
$fetch_top_subs = mysql_query("SELECT * FROM r$round[round]_p WHERE subscription != '". none ."' ORDER BY networth DESC LIMIT 3");
$sub_rank = 0;
while($sub = mysql_fetch_array($fetch_top_subs))
{
$sub_rank++;
if($sub_rank == 1 && $sub_rank['networth'] >= 1)
{
$sub_award = "first_sub";
}
else
if($sub_rank == 2 && $sub_rank['networth'] >= 1)
{
$sub_award = "second_sub";
}
About to test these.
First, i would like you to be more clear with the question. But from what i have understood, this can be the problem with your code:
You are using the $totalKills directly. You can either assign it to a variable and then use it or you need to use $killa for accessing $totalKills, i.e.
Use it as:
$anyVar = $killa['totalKills'];
and then use $anyVar for your operations or directly use $killa['totalKills'] in place of $totalKills.
Hope this helped.
Related
I get an error on line 15 that says "Undefined variable: row2". How can I resolve this?
$limit = 20;
$res1 = mysql_query('SELECT *
FROM contact
WHERE name = "Greg"');
$res2 = mysql_query('SELECT name
FROM contact c, passport p
ON c.idNum = p.iNum
WHERE date >= "2015-03-03" AND t< "2015-03-21');
if(!$res1 && !$res2) {
die('Query no valid: ' . mysql_error());
}
else {
while(($row1 = mysql_fetch_array($res1)) || ($row2 = mysql_fetch_array($res2))) {
$sub = $row1['num'] - $row2['num'];
if($sub <= $limit) {
echo '<br>row name is: ', $row2['name'];
}
}
}
What I'm trying to do is get a number from the first table (it only results to just Greg's row). Then subtract it with the numbers from the results of the second table. The result of this is placed into the sub variable and it's check to see if it's <= 20. If yes, it prints out the row. If not, it goes back to while loop to check another row. Am I going about the right way so far?
You need to change the while() loop's condition. Consider this example:
$a = 1;
if ($a == 1 || $b = 2) {
var_dump(isset($b));
}
Output of var_dump() will be boolean false because $b does not exist, which is the same case why your $row2 is undefined.
The thing is, while evaluation conditions with ||, PHP will stop evaluating other conditions once the match is found, so other comparisons or assignments on the right side will not be performed.
Change your while to be like this, you need both $row1 and $row2 anyway:
while(($row1 = mysql_fetch_array($res1)) && ($row2 = mysql_fetch_array($res2))) {
(note the && instead of ||)
Also, looks like you may want to use SELECT c.* in your second query, too, because you're only selecting the name column, and trying to use num too.
Note : Select all columns in your 2nd Query if num is already available in your columns so your problem will be solved then.!
Note : Try to replace || with && and you will be good to go.
By using || or OR as in conceptional language as I would say it.You are making the code like in a way that either and only one will pass but if you are passing both ones so then you should replace || with && so that's why your $row2 will be already created then so it will be available for more operation.!
$limit = 20;
$res1 = mysql_query('SELECT *
FROM contact
WHERE name = "Greg"');
$res2 = mysql_query('SELECT *
FROM contact c, passport p
ON c.idNum = p.iNum
WHERE date >= "2015-03-03" AND t< "2015-03-21');
if(!$res1 && !$res2) {
die('Query no valid: ' . mysql_error());
}
else {
while(($row1 = mysql_fetch_array($res1)) && ($row2 = mysql_fetch_array($res2))) {
$sub = $row1['num'] - $row2['num'];
if($sub <= $limit) {
echo '<br>row name is: ', $row2['name'];
}
}
}
I have a huge table with some items. I want to retrive specific items from that table according with the players level of gaming . After the code extracts all the specific items i want to add them into an array ( in this case "$cart") . I want to update a mysql row into a varchar field the array separated by a comma ",";
I tried different methods of adding them into the database but something isn't working.
Heres my code :
$sql = mysql_query("select * from iteme where tip != 'Minereu' AND tip != 'Reteta' AND tip != 'Cooking' AND tip != 'Altele' AND tip != 'Potiuni' AND tip != 'Fragment' AND tip != 'Tools' AND tip != 'Medicina' ");
$numaratoare = 0;
$cart = array();
$nivel_player = mysql_query("select * from membri where id !=0 ");
while($informatie_player = mysql_fetch_array($nivel_player))
{
$nivel_actual_player = lvl($informatie_player['experienta']);
$shop_arme_plus_5 = $nivel_actual_player + 5 ;
if ($nivel_actual_player - 5 == 0 )
$shop_arme_minus_5 = $nivel_actual_player - 5 ;
else
$shop_arme_minus_5 = 0 ;
$shop_arme = mysql_query("select * from iteme where tip = 'Arme' AND level_minim < ".$shop_arme_plus_5." AND level_minim > ".$shop_arme_minus_5." AND vandabil = 1 ");
while($informatie = mysql_fetch_array($shop_arme)) {
$numaratoare ++;
//echo " ".$informatie['nume_ro']." , ";
array_push($cart, $informatie['obiect']);
}
mysql_query("INSERT INTO membri_shop_iteme set iteme_arme = ".$informatie["obiect"]." where id_jucator = ".$informatie_player['id']." "); // Here is the problem.
}
Where's the problem? I tried adding implode(', ', $cart); but still i can't add them into database.
Thanks for helping.
may be its string problem $informatie["obiect"] to $informatie['obiect']
mysql_query("INSERT INTO membri_shop_iteme set iteme_arme = ".$informatie['obiect']." where id_jucator = ".$informatie_player['id']." "); // Here is the problem.
to
$newObject=join(",",$cart);
mysql_query("UPDATE membri_shop_iteme SET iteme_arme = '".$newObject."' where id_jucator = ".$informatie_player['id']." "); // Here is the problem.
You use $informatie["obiect"] in your query, but you have to use $cart.
while($informatie = mysql_fetch_array($shop_arme)) {
$numaratoare ++;
array_push($cart, $informatie['obiect']);
}
mysql_query("UPDATE membri_shop_iteme
SET iteme_arme = '".implode("','", $cart)."'
WHERE id_jucator = '".$informatie_player['id']."'");
We have a system on our website wherein you can have multiple accounts and earn points on each. Those points you can transfer between the accounts on this webpage that I've noticed an error on. Basically, if I have a certain combination of accounts, namely a first and second, it won't let me transfer, it'll just say "Please fill in with a number.". If I have a first and third or all three, it works fine. I've been looking through it for about two hours now and can't find what isn't working... any help would be IMMENSELY appreciated :D
<?php if($a == "exchange")
{
$GetUserInfo = mysql_query("SELECT * FROM members WHERE id = '$userid'") or die(mysql_error());
$GetUserInfo = mysql_fetch_object($GetUserInfo);
$cols = 1; //determines colspan
$status = 1;
$GetMultipleInfo = mysql_query("SELECT * FROM members WHERE id = '".$GetUserInfo->mult_uid."'") or die(mysql_error());
if(mysql_num_rows($GetMultipleInfo) != 0)
{
++$cols;
++$status;
}
$GetMultipleInfo = mysql_fetch_object($GetMultipleInfo);
$GetAdMultipleInfo = mysql_query("SELECT * FROM members WHERE id = '".$GetUserInfo->mult_admin."'") or die(mysql_error());
if(mysql_num_rows($GetAdMultipleInfo) != 0)
{
++$cols;
$status = ($status == 2 ? 4 : 3);
}
$GetAdMultipleInfo = mysql_fetch_object($GetAdMultipleInfo);
// Sparks Transfer
if (isset($_POST['spartrans']))
{
$order = $_POST['sparrecipients'];
if ($order == 'first')
{
$tpoints2 = $_POST['tpoints2'];
$tpoints3 = $_POST['tpoints3'];
$tpoints = $tpoints2 + $tpoints3;
if ($status == 2)
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints2)) || empty($tpoints1) || empty($tpoints2))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
elseif ($status == 3)
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints3)) || empty($tpoints1) || empty($tpoints3))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
elseif ($status == 4)
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints2)) || (!is_numeric($tpoints3)) || empty($tpoints1) || empty($tpoints2) || empty($tpoints3))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
if ($tpoints2 > $GetMultipleInfo->tpoints)
message("" . getName($GetMultipleInfo->id) . " does not have enough sparks.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
if ($tpoints3 > $GetAdMultipleInfo->tpoints)
message("" . getName($GetAdMultipleInfo->id) . " does not have enough sparks.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
if ($GetUserInfo->mult_uid != 0)
mysql_query("UPDATE members SET tpoints = GREATEST(tpoints - $tpoints2,0) WHERE id = '".$GetMultipleInfo->id."'") or die(mysql_error());
if ($GetUserInfo->mult_admin != 0)
mysql_query("UPDATE members SET tpoints = GREATEST(tpoints - $tpoints3,0) WHERE id = '".$GetAdMultipleInfo->id."'") or die(mysql_error());
mysql_query("UPDATE members SET tpoints = GREATEST(tpoints + $tpoints,0) WHERE id = '$userid'") or die(mysql_error());
message("Successfully transferred $tpoints Sparks to ".getName($userid).".","Enchanted Hogwarts","$PHP_SELF?a=exchange");
}
}
}
?>
In your code here you are establishing a couple of variables.
if ($order == 'first')
{
$tpoints2 = $_POST['tpoints2'];
$tpoints3 = $_POST['tpoints3'];
$tpoints = $tpoints2 + $tpoints3;
But then here you are checking the values of different variables. $tpoints1 was never established.
if ((!is_numeric($tpoints1)) || (!is_numeric($tpoints2)) || empty($tpoints1) || empty($tpoints2))
message("Please fill in with a number.","Enchanted Hogwarts","$PHP_SELF?a=exchange");
EDIT: That is probably not correct. You probably have register_globals on which would be setting that variable automatically. Not recommended but not the question you asked.
Bottom line you need to figure out which branch of your code is being executed. My guess based on your description is that it is the $order == first branch and then $status == 3. But without actually submitting the form and knowing which selections were made it is impossible for me to tell. And the fact that all the error messages are the same "Please fill in with a number." is not very helpful.
I personally would take this time to refactor some of your code. If I don't understand something I break it apart into smaller chunks until I do. Otherwise you will always be sifting through this page every time something is wrong trying to figure it out.
Every selection of $order validates the values of $tpoints1, $tpoints2, and $tpoints3 exactly the same way. Start by moving that into a function and go from there.
I feel this is a more logic problem than anything. A database has pictures saved via a source reference and booleans for tags e.g. isLandscape=1. I had made a system to traverse pages of results based on types asked. The following is an example of what I'm facing. I only see the same 12 pictures from page 0 -> page 22. Then I start to see new ones. I think I have just been overlooking this bug since I had not noticed it until now. One thing I noticed was page22*12pictures = 264 which is the same as the first new picture id that is seen. You can see the error here (just change the p to different pages).
<?php
$pictureid = -1;
$startpage = 0;
$viewsection = -1;
$uid = -1; //user id
$amntperrow = 4; //how many pictures per row, must correlate with doThumb()'s switch case amounts
$maxrows = 3; //how many rows of pictures to drop
if(isset($_GET['pid']) && is_int(intval($_GET['pid']))) $pictureid = clean($_GET['pid']);
if(isset($_GET['sec']) && is_int(intval($_GET['sec']))) $viewsection = clean($_GET['sec']);
if(isset($_GET['p']) && is_int(intval($_GET['p']))) $startpage = clean($_GET['p']);
$result = generateResult(array("isFlowers"), $startpage);
//**snip** -- drawing thumbnails would happen here
function generateResult($types, $page) {
global $amntperrow;
global $maxrows;
$sqlWheres = "";
$idAmnt = ($amntperrow*$maxrows)*$page;
if(isset($types) && !empty($types)) {
if(count($types) >= 1) {
for($i = 0; $i<count($types); $i++) {
$sqlWheres .= $types[$i] . "='1'";
if($i < count($types)-1) $sqlWheres .= " AND ";
}
}
}
$result = "SELECT * FROM pictures WHERE ";
if(!empty($sqlWheres)) $result .= $sqlWheres . " AND " ;
$result .= " private='0' AND id >='" . $idAmnt . "' LIMIT " . ($amntperrow*$maxrows);
return $result;
}
?>
This seems like a glaring bug that I am overlooking. Thanks for the help.
What is the difference between these two queries?
SELECT *
FROM pictures
WHERE private = '0' AND id >= '24'
LIMIT 12;
and
SELECT *
FROM pictures
WHERE private = '0' AND id >= '36'
LIMIT 12;
Answer: potentially no difference at all. The database engine can decide in either case that it wants to return pictures with ids 100 through 111 - that result set meets all of the conditions of either query.
Try a query like this instead:
"SELECT *
FROM pictures
WHERE private = '0'
ORDER BY id
LIMIT " . $idAmnt . ", " . ($amntperrow * $maxrows)
The ORDER BY id is really the key. Paging through database results is generally done with a combination of ORDER BY and LIMIT.
I have a rating system that uses the following equation to generate the rating average:
((Old Rating*Old Times Amount)+New Rating)/New Rating amount
However, if the current rating is 3, and it's been rated once, when I rate it three, it says the new rating is 2.5
What is the error here? Here's the full code.
<?php
session_start();
include("lib/db.php");
$db = new DBConnect;
if(isset($_POST['rating']) && is_numeric($_POST['rating']) && is_numeric($_POST['story']))
{
if($_POST['rating'] > 5 || $_POST['rating'] < 1){die("INVALID RATING");}
$rating = mysql_real_escape_string($_POST['rating']);
$story = mysql_real_escape_string($_POST['story']);
$c = $db->query("SELECT * FROM cdb_stories WHERE id=$story");
$c = mysql_fetch_array($c);
$u_name = mysql_real_escape_string($_SESSION['logged_in']);
$uid = $db->query("SELECT id FROM cdb_users WHERE username='{$u_name}'");
if(mysql_num_rows($uid) < 1){die("NOT LOGGED IN");}
$uid = mysql_fetch_array($uid);
$ratingd = $db->query("SELECT * FROM cdb_ratings WHERE userid='{$uid['id']}'");
if(mysql_num_rows($ratingd) > 0)
{
$ratingd = mysql_fetch_array($ratingd);
$new_rate = (($c['rating']*$c['rating_amt'])-$ratingd['rating']+$rating)/$c['rating_amt'];
$db->query("UPDATE cdb_stories SET rating={$new_rate} WHERE id={$story}");
$db->query("UPDATE cdb_ratings SET rating={$rating} WHERE userid='{$uid['id']}'");
die();
}
$new_num = $c['rating_amt']+1;
$new_rate = (($c['rating']*$c['rating_amt'])+$rating)/$new_num;
$db->query("UPDATE cdb_stories SET rating_amt={$new_num}, rating={$new_rate} WHERE id={$story}");
$db->query("INSERT INTO cdb_ratings VALUES({$uid['id']},{$rating},{$story})");
}
else
{
die("INVALID FIELDS");
}
?>
((Rating * Times) + New) / (Times + 1)
For your values:
((3 * 1) + 3) / (1 + 1)
= ( 3 + 3) / 2
= 6 / 2
= 3
So the procedure looks mathematically correct.
I suggest you put the calculation into a function of it's own with parameters, so you don't get irritated so much by the rest of the code you have in that batch. This will make it easier to debug for you:
function new_rate($rating, $times, $new)
{
return (($rating * $times) + $new) / ($times + 1);
}
You can then use that more easily within your code. Additionally if something else is the cause of the error, you can simply find out by testing the bare function. If it acts correct, you know that the error is placed somewhere else.
Hope this helps.