PHP + SQL -- Counting clicks in multiple rows - php

i stuck at the problem that my code wont count total clicks in multiple rows.
This is how the Database looks
id | jb_clicks | created_time
--------------------------------------------------
1 | 14 | 1475420816
2 | 7 | 1475422200
3 | 9 | 1475422217
4 | 3 | 1475422239
I want the result to be 33 (14+7+9+3)
How is this possible?
The current code looks like this:
SELECT COUNT(*) AS jb_clicks FROM jb_urls
my function:
function sumdatabase($select, $statement){
$config = new mysql_config;
$link = mysqli_connect($config::MYSQL_HOST,$config::MYSQL_USER,$config::MYSQL_PASS);
mysqli_select_db($link, $config::MYSQL_DATABASE);
$result = mysqli_query($link,"SELECT SUM(".$config::MYSQL_PREFIX."$select) AS jb_clicks FROM $statement");
$row = mysqli_fetch_assoc($link,$result);
return $row[$config::MYSQL_PREFIX.$select];
}

Use the following:
SELECT SUM(jb_clicks) AS jb_clicks FROM jb_urls
COUNT() is used to count the row numbers and SUM() is to calculate the total sum of a column in a table. In your case, it would be SUM(jb_clicks).

Related

do the math for each data in database sql post result to database in php

I'd like to fetch data from my 2 sql database and do some math and post the result in database
let's say my table1 is like this
+---+---+----------------------------+
| A | B | C |
+---+---+----------------------------+
| 2 | 9 | result from A*B*D*E in php |
| 1 | 8 | result from A*B*D*E in php |
| 4 | 7 | result from A*B*D*E in php |
| 3 | 6 | result from A*B*D*E in php |
| 6 | 5 | result from A*B*D*E in php |
| 6 | 5 | result from A*B*D*E in php |
| 5 | 4 | result from A*B*D*E in php |
+---+---+----------------------------+
and my table2 is like this
+---+----+
| D | E |
+---+----+
| 1 | 9 |
| 2 | 7 |
| 3 | 8 |
| 4 | 6 |
| 5 | 5 |
| 6 | 3 |
| 7 | 2 |
+---+----+
so far what i've done
// database connection
include_once("config.php");
// Query
$query = mysqli_query($conn, "SELECT * FROM table1");
$query2 = mysqli_query($conn, "SELECT * FROM table2");
//Source1
while($user_data1 = mysqli_fetch_array($query))
{
$A[] = $user_data1['A'];
$B[] = $user_data1['B'];
}
//Source2
while($user_data2 = mysqli_fetch_array($query2))
{
$D[] = $user_data2['D'];
$E[] = $user_data2['E'];
}
foreach (array_combine($A, $B) as $ValueA=> $ValueB)
{
foreach (array_combine($D, $E) as $ValueD=> $ValueE)
{
$result1 = $ValueA*$ValueB*ValueD*ValueE;
$val = 0.123;
$result2[] = $result1*$val;
}
$final result = min($result2);
echo round($final result, 2);
unset($result2);
}
I haven't inserted the database yet
still echoing for debug if the math is correct
somehow this code found some bug
for example using my database the final result only echo/showing 6 math result
because in table1 row 5 and 6 has same data
btw of course in my table1 and 2 has primary key
To change C in this case, you don't even need PHP. To UPDATE a value in MySQL with multiple tables just add them with a , when selecting the tables, like this:
UPDATE table1,table2 SET C = table1.A * table1.B * table2.D * table2.E WHERE C IS NULL;
Executing this code once will update all rows so that C = A*B*D*E as wanted where C is not yet set or NULL. If you want to update all rows you can just remove the WHERE condition
Note: Sometimes (at least for me) SQL will give a warning when having no WHERE condition in the SQL query. To bypass this just add WHERE 1=1 at the end.
Just for my understanding: you want to calculate a value for your calculation you need some data from table 1 that is clear, but also from table2 But which one? I guess you want to use the data from the same row ( so row 1 from table1 and row 1 from table2, row 2 from table 1 and row 2 from table2 ) right? Now you have an problem because when you make a select * from table You do not know in which order they give back from your database. Most time it may be the same order as you have input them, but there is no garantie. You have sayed you have an primary key on each table, how have you defined them? I guess you may have a id column, so you can join your table on that id?

Display some data from row and column

I'm trying to display first and second result from table, so that user can see both results on profil.php and the date from last result. I created a view vwresult that has these fields
id | ide | name | mark | date
---+-----+---------+------+-----------
1 | 1 | trickpd | 3 | 06.01.2018
1 | 2 | trickpd | 2 | 03.01.2018
5 | 3 | trickpd | 4 | 08.01.2018
5 | 4 | trickpd | 6 | 02.01.2018
That is my table with current result, insert new data in table will show more results in view table.
This is my code
$tst = mysqli_query($mysqli, "SELECT * FROM vwresult WHERE id=$ig AND name='trickpd' ORDER BY id, date desc");
$marks = [];
while($tstx = mysqli_fetch_assoc($tst)) {
$marks[] = $tstx['mark'];
}
After that I get stuck, and can't show the results.
The goal is:
to show mark 3 and 2 and date 06.01.2018 for id 1,
and when user opens details for id 5 to see results the will see 4, and 6 and also a 08.01.2018
Please help. Thank you all for your time.
Problem is with displaying the data.. I even create a two query one to show all the data and second with limit 1,1 but after that on script it's showing random data, not first and second result like I wont.. I'm trying to display last two results and the last date of inserting the data..

Displays WHERE for two conditions in the same column

-----------------------------------------------------
| id | posts_id | users_id | ratings |
-----------------------------------------------------
| 1 | 7 | 20 | 5 |
| 2 | 8 | 20 | 3 |
| 3 | 7 | 21 | 4 |
-----------------------------------------------------
Table name: mytable
\I want to make sure that ratings between posts_id and users_id are matched on the same column.
$query = $conn->query("SELECT ratings FROM mytable WHERE posts_id=7 and users_id=20");
$row = $query->fetch_array();
echo $row['ratings'];
This query does not work. I know there must be something wrong.
I want to get results: 5
What is the best query to show ratings?
----------------UPDATE-----------------------------
Sorry, my first problem lies with the connection, and now it is resolved.
But now there is a new problem.
I want to display the total sum of the rating results.
My new code
$Rate = $conn->query("SELECT * FROM mytable WHERE posts_id=7");
while ($Rated = $Rate->fetch_array()) {
echo $Rated['ratings'] + $Rated['ratings'];
}
For example on posts_id=7
Here I expect 5 + 4 = 9
But my code results exactly 54 + 54
How to fix this code?
For the updated question, this code should be work. We can use sum() function. Check here sum() function
$Rate = $conn->query("SELECT sum(ratings) as ratings FROM mytable WHERE posts_id=7");
while ($Rated = $Rate->fetch_array()) {
echo $Rated['ratings'];
}

Codeigniter. How to get sum of database columns

I have two tables .
pre_order
receive_pre_order
My Question is how to get all the information from pre_order table and get the SUM of all the quantity received form receive_pre_order table against that pre_order id.
To make it clear suppose we have the following data in recieve_pre_order .
+--------------+------------+-------------------+
| pre_order_id | product_id | quantity_recieved |
+--------------+------------+-------------------+
| 1 | 1 | 10 |
| 1 | 1 | 20 |
| 1 | 1 | 10 |
+--------------+------------+-------------------+
It should return quantity_recieved = 40 against pre_order_id 1 .
Right now I have tried:
$this->db->select_sum('quantity_recieved');
$this->db->from('recieve_pre_order');
return $this->db->get();
It is working but, it is getting data from one table.
But I want to fetch data from both tables.
Join the two tables tables (rpo.pre_order_id = po.id). Group the result for po.id, so all Rows with the same po.id will be grouped into one row. sum(rpo.quantity_recieved) will sum up the quantity for these groups.
select po.id, sum(rpo.quantity_recieved)
from receive_pre_order rpo
join pre_order po on rpo.pre_order_id = po.id
group by po.id;
try this
$this->db->select("sum(recieve_pre_order.quantity_recieved),pre_order.id");
$this->db->from('recieve_pre_order');
$this->db->join("pre_order",'recieve_pre_order.pre_order_id = pre_order.id','left outer');
$this->db->where('recieve_pre_order.pre_order_id',your parameter);
$this->db->select('pre_order.*');
$this->db->select_sum('recieve_pre_order.quantity_recieved');
$this->db->from('pre_order');
$this->db->join("recieve_pre_order",'recieve_pre_order.pre_order_id = pre_order.id','left');
try sub query instead of joining table.
$this->db->select("po.*,SELECT(SUM(quantity_recieved) from receive_pre_order where pre_order_id = po.id) as total_recieved_quantity",FALSE);
$this->db->from('pre_order as po');
return $this->db->get();

Column Calculator

I have the following table and want to know how to set up a column total to calculate the sum of the row. I want the total column to calculate first_bid - second_bid.
id | First Bid | Second Bid | Total
0 | 7 | 1 | (first_bid)-(second_bid)
1 | 8 | 2 |
2 | 5 | 3 |
3 | 4 | 4 |
4 | 5 | 5 |
5 | 5 | 6 |
I need to display to the user all previous bids and total on a page. The total should be in descending order also.
SELECT id, First_Bid, Second_Bid, First_Bid - Second_Bid AS Total
For the grand total, you'd have to run a second query with SUM() aggregate functions. Which is somewhat redundant, since you can just do the summation in your client as you retrieve the data. e.g.
$total = 0;
while($row = fetch_from_db($result)) {
$total += $result['Total'];
... display row data ...
}
... display total ...
You can just calculate the total when you are querying the database.
SELECT first_bid, second_bid, (first_bid - second_bid) as total FROM table ORDER BY 3 DESC
Something along these lines
Try this query:
SELECT *, (First Bid-Second Bid) as total from TABLE

Categories