$dt = mysql_query("SELECT * FROM `member_territory` mt LEFT JOIN `drug_territory` dt ON mt.mt_ter = dt.t_id");
while($t = mysql_fetch_array($dt)) {
$total +=($t['t_reward']* $t['mt_lev'])*150;
mysql_query("UPDATE `members` SET
`drug_income` = '".$total."',
`drug_incometotal` = `drug_incometotal` + '".$total."',
`wallet` = `wallet` + '".$total."'
WHERE `playerid` = '".$t['mt_playerid']."'");
}
So here is my code rather self explainingtary $total when inserted into drug_income that is correct but when ever it is inserted into drug_incometotal or wallet its incorrect.
im not sure why and i have tried everything to my knowledge to pull my head around it!!.
Any ideas why i am getting this incorrect result ( as i say drug_income is correct) only when i try to '+' it to something in the data base it returns an incorrect result.
i dont want to increment just once after the loop... – user3740302 1 min ago
Okay then. So you should probably move your UPDATE query outside of the loop, right?
You may try this modified update query
mysql_query("UPDATE `members` SET drug_income = ".$total.", drug_incometotal = drug_incometotal + ".$total.", wallet = wallet + ".$total." WHERE `playerid` = '".$t['mt_playerid']."'");
It may resolve your problem..
Related
I have one table based on which one I have to update 6 rows in the other table for matching ids. It is total of over 1000 records so most of the time I get timeout error with current script.
The way I do it now is, I select the range of ids between two dates from the first table, store it into an array and then run foreach loop making update in the second table where the ids are the same, so basically I run a query for every single id.
Is there anyway I could speed it up the process?
I found only a way to generate the each within the foreach loop
UPDATE product SET price = CASE
WHEN ID = $ID1 THEN $price1
WHEN ID = $ID1 THEN $price2
END
But I don't know how could I modify this to update multiple rows at the same time not just one.
My script code look like that
$sql = "SELECT * FROM `games` where (ev_tstamp >= '".$timestamp1."' and ev_tstamp <= '".$timestamp2."')";
while($row = mysqli_fetch_array($sql1)){
$one_of =[
"fix_id" =>$row['fix_id'],
"t1_res" =>$row['t1_res'],
"t2_res" =>$row['t2_res'],
"ht_res_t1" =>$row['ht_res_t1'],
"ht_res_t2" =>$row['ht_res_t2'],
"y_card_t1" =>$row['y_card_t1'],
"y_card_t2" =>$row['y_card_t2'],
"t1_corners" =>$row['t1_corners'],
"t2_corners" =>$row['t2_corners'],
"red_card_t1" =>$row['red_card_t1'],
"red_card_t2" =>$row['red_card_t2']
];
array_push($today_games,$one_of);
}
foreach($today_games as $key=>$val){
$cards_t1=$val['red_card_t1']+$val['y_card_t1'];
$cards_t2=$val['red_card_t2']+$val['y_card_t2'];
$sql = "Update sights SET t1_res='".$val['t1_res']."',
t2_res='".$val['t2_res']."', ev_tstamp='".$val['ev_tstamp']."',
ht_res_t1='".$val['ht_res_t1']."', ht_res_t2='".$val['ht_res_t2']."',
t1_corners='".$val['t1_corners']."',t2_corners='".$val['t2_corners']."',
t1_cards='".$cards_t1."',t2_cards='".$cards_t2."'
where fix_id='".$val['fix_id']."' "
}
Consider an UPDATE...JOIN query using fix_id as join column. Below runs mysqli parameterized query using timestamps. No loop needed.
$sql = "UPDATE sights s
INNER JOIN `games` g
ON s.fix_id = g.fix_id
AND g.ev_tstamp >= ? and g.ev_tstamp <= ?
SET s.t1_res. = g.t1_res,
s.t2_res. = g.t2_res,
s.ev_tstamp = g.ev_tstamp,
s.ht_res_t1 = g.ht_res_t1,
s.ht_res_t2 = g.ht_res_t2,
s.t1_corners = g.t1_corners,
s.t2_corners = g.t2_corners,
s.t1_cards = (g.red_card_t1 + g.y_card_t1),
s.t2_cards = (g.red_card_t2 + g.y_card_t2)";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, 'ss', $timestamp1, $timestamp2);
mysqli_stmt_execute($stmt);
I am trying to have my function increment by 1 a value in mysql, but it only works for once - from zero to 1. Then it won't increment. I have noticed that I need to add (int) in order to get it do this, so I guess there is something to do with data type. Do you have any hints?
$mod_seq = $this->db2->query("select mod_sequence from tbl_release_info where release_id = '$release_id';");
$current_modsequence = (int)$mod_seq++;
$update_query_release_info = "update tbl_release_info set mod_sequence = '$current_modsequence' where release_id = '$rid';
You can do that with just SQL and in a single query like this
$this->db2->query("update tbl_release_info
set mod_sequence = mod_sequence + 1
where release_id = '$rid'");
The query in your question returns the number of rows, not the value you want from mod_sequence. Try this instead:
$query = $this->db2->query("select mod_sequence from tbl_release_info where release_id = '$release_id';");
$row = $query->row();
if (isset($row)) {
$mod_seq = $row->mod_sequence;
}
$mod_seq++;
$update_query_release_info = "update tbl_release_info set mod_sequence = '$mod_seq' where release_id = '$rid';
If the datatype of mod_sequence is not an integer, type cast it:
$mod_seq = (int)$row->mod_sequence;
Or like this:
$mod_seq = intval($row->mod_sequence);
EDIT: #RiggsFolly nailed the most elegant solution, by far, in a single query :-) https://stackoverflow.com/a/42194758/1363190
I am trying to make two queries providing $result and $result2. I would like to subtract the two results to create a total for the vote (negative vote being possible). Therefore on an empty query the result value would have to be zero.
My subtraction function isn't working. Does anyone know what I am doing wrong? I am getting no result.
Thanks in advance!
My code:
$result = mysql_query('SELECT COUNT(*) FROM comment_votes WHERE vote_com_writer_id = "'.$d['user_id'].'" AND vote_com_rank = "1"');
$positive_votes = mysql_result($result, 0);
$result2 = mysql_query('SELECT COUNT(*) FROM comment_votes WHERE vote_com_writer_id = "'.$d['user_id'].'" AND vote_com_rank = "2"');
$negative_votes = mysql_result($result2, 0);
list($vote) = $positive_votes - $negative_votes;
Start with the basics.
Confirm that your sql queries work by manually executing them against your database.
Confirm that $postive_votes and $negative_votes are what you expect with something like the following:
echo 'Positive Votes: '.$positive_votes.', Negative Votes: '.$negative_votes.
remove list($vote) as it is not being used correctly. The list function is intended for arrays not the results of subtraction. Replace that line with the following:
$total_votes = $positive_votes - $negative_votes;
echo 'Total Votes:'.$total_votes;
.
I'm introducing myself to PDO and i'm trying to fetch data with it. I've done this before, but now i'm getting errors all the time. I've been through this for some hours and didn't find a mistake. If someone could help: my code is:
$tabelas_intervalos_afunda = ($con -> query('CREATE TABLE IF NOT EXISTS afunda_$a
SELECT (L1_forma_tensao_max + L1_forma_tensao_min)/2 as L1_forma_tensao, (L2_forma_tensao_max + L2_forma_tensao_min)/2 as L2_forma_tensao, (L3_forma_tensao_max + L3_forma_tensao_min)/2 as L3_forma_tensao
FROM afundamento
WHERE id > $prevNum AND id < $a');
while($row=$tabelas_intervalos_afunda->fetch(PDO::FETCH_ASSOC))
{
$array_forma_onda_fase1_afund[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund[] = $row['L3_forma_tensao'];
}
My problem is that when i var_dump($array_forma_onda_fase1_afund), it returns me "Undefined variable $array_forma_onda_fase1_afund
NULL
Some additional info: $a is changed always when the loop condition is satisfied. The table afunda_$a is being created as expected, table afundamento exists normally.
Would appreciate any help/suggestions.
You are running a create table statement, which returns no rows. Then running fetch on the result, which doesn't enter the while statement. Hence $array_forma_onda_fase1_afund is never defined.
If you want the records you are inserting, you can select them out of the new table or run the original select query. E.g.:
$con->query('CREATE TABLE IF NOT EXISTS afunda_$a
SELECT (L1_forma_tensao_max + L1_forma_tensao_min)/2 as L1_forma_tensao, (L2_forma_tensao_max + L2_forma_tensao_min)/2 as L2_forma_tensao, (L3_forma_tensao_max + L3_forma_tensao_min)/2 as L3_forma_tensao
FROM afundamento
WHERE id > $prevNum AND id < $a');
$tabelas_intervalos_afunda = $con->query("SELECT * FROM afunda_$a");
while($row=$tabelas_intervalos_afunda->fetch(PDO::FETCH_ASSOC))
{
$array_forma_onda_fase1_afund[] = $row['L1_forma_tensao'];
$array_forma_onda_fase2_afund[] = $row['L2_forma_tensao'];
$array_forma_onda_fase3_afund[] = $row['L3_forma_tensao'];
}
Currently, I have the following PHP code loaded every time the page is refreshed. I am trying to update the views column +1 every time the page is loaded. To do this, I first retrive the previous views value from the table, then run another query to add + to that number. The problem that is occurring is every time I refresh the page, The code somehow adds two instead of 1. So instead of the $viewsA variable increasing by +1, it is increasing by +2.
$query = mysql_query("SELECT * FROM Games WHERE pagename = '$game' ");
WHILE($datarows = mysql_fetch_array($query)):
$title = $datarows['title'];
$description = $datarows['desc'];
$img_url = $datarows['img'];
$cat = $datarows['cat'];
$pagename = $datarows['page'];
$rating = $datarows['rat'];
$viewsA = $datarows['view_count'];
$gameid = $datarows['id'];
endwhile;
$updateviews = $viewsA +1;
mysql_query("UPDATE `trainw_games`.`Games` SET `view_count` = '$updateviews' WHERE `Games`.`id` = $gameid;");
What do I need to change to make it only add +1 to the views column?
I don't think that a while loop is appropriate for this problem. I would recommend to echo $viewsA . '-' . $updateviews; to see what the value is before and after the add.
But, why not just run a single UPDATE statement?
UPDATE Games SET view_count = view_count + 1 WHERE Games.id = $gameid
Of course, you should stop using mysql_ functions and use either MySQLi or PDO:
$stmt = $mysqli->prepare("UPDATE Games SET view_count = view_count + 1 WHERE Games.id = ?");
$stmt->bind_param($gameid);
$stmt->execute();
$stmt->close();
Do you have multiple rows in your database? If yes, it maybe caused by overriding the previous value.
For example:-
If first row the view_count is 1.
While the second row view_count is 2.
that overrides the the first row with 2 + 1 = 3
Which makes you thought increased by 2?
UPDATE 1:
Ok, try this, put this
$updateviews1 = $viewsA + 1;
if ($viewsA < $updateviews1) { //execute the viewsA + 1 }