PHP Calculation from database not calculating correctly. [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to run a sum from numbers taken from a database. However i can't seem to get it to work.
Does anyone have any ideas as to why i keep getting 0 instead of a percentage? if so could you guide me in the right direction.
PHP:
//SQL Original Goal
$goal = "SELECT * FROM login WHERE username = '$login'";
$goalquery = mysql_query($goal);
$goalarray = mysql_fetch_array($goalquery);
$endgoal = $goalarray['goal'];
//Latest Weight
$latestweightarray = mysql_fetch_array($latestweightq);
$currentweight = $latestweightarray['weight'];
//First Weight Recorded
$firstweightarray = mysql_fetch_array($firstweightq);
$firstweight = $firstweightarray['weight'];
$percent = "100";
$progress = (($firstweight - $currentweight) / ($firstweight - $endgoal)) * $percent;
Print Result:
<?php
echo $progress
?>
This just gives: 0

Rookie Error, i had updated the database twice in one day which conflicted the results somehow, changed one of the dates in the database and this solved the problem

Related

foreach loop with if statements [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 days ago.
Improve this question
<?php
$income = array(19500, 22000, 85205, 110000, 26568, 12000, 37500, 50900, 28000, 32430);
$percentage = 0.09;
foreach($income as $integers)
{
echo ("$integers <br/>");
}
if ($integers < 26568);
{
echo "$integers = Pay back £0 per year";
}
?>
outcome below
19500
22000
85205
110000
26568
12000
37500
50900
28000
32430
32430 = Pay back £0 per year
If it is lower than 26568 then next to the number I want it to say "Pay back £0 per year" but if it's more than 26568 I need to calculate 9 percentage and add how much is paid back next to the number. Is this possible?
I tried the above and keep getting the same output, I need to be able to calculate the 9 percent and add this next to the number.

PDO Prepared Statements How to Select From Database Where Id = Value or Tell = Value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to Select one record from a table Named 'sms_students' by using Student Id or Student Telephone
Here is My Code
$student_rand_id=$_GET['std_rand'];
$std_tell=$_GET['std_tell'];
$view_student = $config->prepare("SELECT * FROM sms_students WHERE st_rand = :random_id || st_tel =: st_tel");
$view_student->execute(['random_id' => $student_rand_id]);
$view_student->execute(['st_tel' => $std_tell]);
$row = $view_student->fetch();
Since you call execute twice, this executes twice, both times with an incomplete set of arguments. It's an easy fix though:
$view_student = $config->prepare("SELECT * FROM sms_students WHERE st_rand = :random_id OR st_tel = :st_tel");
$view_student->execute(['random_id' => $_GET['std_rand'], 'st_tel' => $_GET['std_tell'] ]);
$row = $view_student->fetch();
Try and get rid of single-use variables, they're almost always unnecessary, and do try and steer towards having names that match precisely. Seeing st_tel and std_tell together is a sign something's not quite right. Get your code to agree on names and stick with them.

Query doesn't update correctly in mySql [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm having problems with a query that should "rank an user to 3" but instead in the MySQL it gives to the user X rank 0.
Here is the code
if($_POST['rank'] == '3')
{
mysql_query("UPDATE users SET rank='3' AND LPT='1' where username='".$_post['u_name']."' LIMIT 1")or die(mysql_error());
$query = true;
}
Waiting for answers
Use comma instead of AND when updating multiple columns:
UPDATE users
SET rank='3',
LPT='1'
where username= ?
You should:
Use parametrized query instead of concatenating SQL string
$_post is a superglobal and must be in uppercase $_POST
Reference: http://php.net/manual/en/language.variables.superglobals.php
Additional reference:
UPDATE http://dev.mysql.com/doc/refman/5.7/en/update.html

Simple MySQL UPDATE only affects certain rows [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
SOLVED
Rows 1, 4, and 10 have a field with a single quote character in them, so I was SQL injecting myself. More reason to switch to prepared statements!
I have a very simple bulk UPDATE query sent from my PHP form which, oddly enough, only affects certain rows. Rows 1, 4, and 10 doesn't get updated; but all other rows get updated.
Here is the query:
$query = "SELECT * FROM SkillDescriptions";
$result = mysql_query($query);
for ($i=1; $i<=mysql_num_rows($result); $i++){
$skillKey = 'Skill' . (string)$i;
$categoryKey = 'Category' . (string)$i;
$descriptionKey = 'Description' . (string)$i;
$sql="UPDATE SkillDescriptions SET
Skill='$_POST[$skillKey]',
Category='$_POST[$categoryKey]',
Description='$_POST[$descriptionKey]'
WHERE id='$i'
";
$result2=mysql_query($sql);
}
I've got a parallel form that does the exact same PHP form processing as this one but has a different database table, which works properly: so the problem most likely lies in the database table (configuration?) and not the code.
Why do only certain rows get updated, in a seemingly random pattern?
UPDATE
Before:
http://i.stack.imgur.com/2hk2l.png
After: (I appended test to the columns)
http://i.stack.imgur.com/ObMn5.png

how to select all table records except last 2? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am using Yii, I want to select all records of a model except the last 2 records, but I got an error that there is syntax error, here is my code:
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents) FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents))";
$model2 = Newsevents::model()->findAll($c);
This one considers, that idNewsEvents might not be sequential (due to deletes or whatever).
$c = new CDbCriteria();
$c->select = "*";
$c->condition = "idNewsEvents NOT IN (SELECT idNewsEvents FROM newsevents ORDER BY idNewsEvents DESC LIMIT 2)";
$model2 = Newsevents::model()->findAll($c);
P.S.: Not sure if this is correct yii syntax, I'm not familiar with it, but I'm very familiar with MySQL. Anyway, you get the idea I hope.
Two checking can be done in a single checking
$c->condition ="idNewsEvents < (SELECT MAX(idNewsEvents)-1 FROM newsevents)";
Example query
SELECT * FROM `users` WHERE user_id < (SELECT MAX(user_id)-1 FROM users)
it should be:
$c->condition = "idNewsEvents!=(SELECT MAX(idNewsEvents)
FROM newsevents) AND idNewsEvents!=(SELECT MAX(idNewsEvents)-1 FROM newsevents)";
There were just an extra bracket at end and a very tired eyes to notice that :( .

Categories