Can't insert numbers with decimal [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have a problem with inserting decimal numbers. The code is a "earning system" that calculate salary. The query insert 2 instead of 2.50.
My code looks like this:
$quantity = "1";
$earned = $quantity * '2.5'; //type = double
//.. query update to table etc..
Mysql:
`earned` decimal(10,2) NOT NULL DEFAULT '0.00'
I tried to echo $earned, and it says 2,5 instead of 2.5 would that be the problem??
Solution:
I removed my setlocale

use number type values instead of strings,try this..
$quantity = 1;
$earned = $quantity * 2.5;

This is a long shot, since you didn't post your actual mysql query, but most likely the 2.5 value is converted in 2,5 by your locale settings.
The easiest solution is to quote all the values you are putting inside your query, so you won't have any side-effects

Related

SUM OF NUMBERS IN WHILE LOOP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
I'm trying to find the sum of numbers by querying a database using PHP and MYSQL. I need a while loop or any other solution that can add numbers that are >= 40 from a sequence of numbers. I'm only able to make the query but don't know how to go about the rest.
$selectMARK="SELECT mark FROM resultstbl WHERE mark >= 40";
$queryMARK=mysqli_query($dbCon, $select);
$sum=0;
while($rowz=mysqli_fetch_assoc($queryMARK))
{
// need some code to add the numbers here
}
You maybe need an
$iterator = 0;
outside the loop, that you can increment inside of it.
$sum += $rowz[$iterator];
$iterator++:
Then do your condition around it by checking $rowz[$iterator] <= 40

How to get sum for the values inside specific column? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a table "selled" which has three column id, qty, code how to sum all the values in qty column where code = X, I know how to count the number of rows in this column but I want to get the sum of the values inside these rows.
I am sorry guys i am still beginner and i really appreciate it ,This is the code i am using and it still not working
$query="SELECT SUM (qty) as sum_of_qty from selled WHERE code='$code'";
$result=mysqli_query($connection,$query);
$row=mysqli_fetch_assoc($result);
echo $row['sum_of_qty'];
Use the SUM function. Docs:
https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_sum
SELECT SUM(qty) as sum_of_qty FROM selled WHERE code = x

The database returns only the first row instead of column [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to obtain a column name EmailId from the table named Users. But it returns only the first string and nothing else?
PHP:
$sql="SELECT EmailId FROM Users;";
$result =mysqli_query($conn,$sql);
$col=mysqli_fetch_array($result);
The database has 3 email-ids but count($col) returns only 1. Why so?
Thanks in advance.
The database has 3 email-ids but count($col) returns only 1. Why so?
Because
$col=mysqli_fetch_array($result);
only fetch the first row according to the internal pointer of the dataset, and therefore
count($col)
returns 1 because $col is an array having 1 item.

How to get a percent value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Okay, so for my website, I made a tests system, where you can take quizes. It all works, but getting the percentage of correct answers is broken.
I have 3 variables I am trying to use to get the percentage.
$incorrect // Int value of incorrect answers
$correct // Int value of correct answers
$perc // Percentage of correct answers from 0 to 100
Currently, I use this, but it does not work:
if($incorrect===0){
$perc = 100;
}elseif($correct===0){
$perc = 0;
}else{
$perc = ($incorrect / $correct)*100;
}
$perc = $correct*100/($incorrect+$correct)
User1950001, as Barmar mentioned, you need to calculate the TOTAL number of answers as a percentage is calculated by (NUMBER OUT OF TOTAL x 100 / TOTAL).
In this case, CORRECT. is your NUMBER OUT OF TOTAL and INCORRECT + CORRECT is your TOTAL, which is what you were missing.
With your original code, perc = ($incorrect/$correct) * 100 where total questions is 25, 20 are incorrect and 5 are correct would return 400% as the value. Obviously this is way off.

check array for limitations [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am accepting form submissions.
To prevent spoofing of the form I am using php isnumeric to verify that the posted values contain only numeric values.
I would like to check also:
1) that the posted values array contains max 1000 values (because no user would buy more than 1000 items!)
2) that the size of a single array key is composed of max 20 numbers (bigint unsigned max length)
how do I achieve this?
Point 1:
if (count($posted_values) <= 1000)
{
...
}
else
echo "Error";
Point 2:
Did you mean PHP_INT_MAX ?
If yes, just do:
foreach($posted_values as $value)
and then check that $value is less or equal than PHP_INT_MAX.

Categories