in my current project I want to split 10 users from a queue into 2 teams with 5 users each.
When 10 users are in the queue the function create_new_lobby() is executed. In this function the match is created and all users should be assigned to the teams in an intermediate table. With a for loop all users should be assigned to the match. Currently the for loop only takes every second user (0, 2, 4, 6, 8). The values 1, 3, 5, 7 & 9 are apparently skipped by the for loop.
Where is the error here?
<?php
function create_new_lobby() {
global $connection;
$timestamp = date("Y-m-d H:i:s", strtotime(date('h:i:sa')));
$stmt = $connection->prepare("INSERT INTO competition_game (create_time) VALUES (?)");
$stmt->bind_param("s", $timestamp);
$stmt->execute();
$stmt->close();
$stmt = $connection->prepare("SELECT competition_match_id FROM competition_game ORDER BY competition_match_id DESC LIMIT 0, 1");
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$competition_match_id = $row['competition_match_id'];
$stmt->close();
for($player = 0; $player <= 9; $player++) {
$status_queue = 1;
$stmt = $connection->prepare("SELECT * FROM competition_queue WHERE competition_queue_status = ? AND DATE_ADD(competition_queue_activity ,INTERVAL 20 MINUTE) >= NOW() ORDER BY competition_queue_id ASC LIMIT ?, 1");
$stmt->bind_param("ss", $status_queue, $player);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
$user_id = $row['user_id'];
$stmt1 = $connection->prepare("INSERT INTO competition_game_player (competition_match_id, user_id) VALUES (?, ?)");
$stmt1->bind_param("ss", $competition_match_id, $user_id);
$stmt1->execute();
$stmt1->close();
}
$stmt->close();
$status_leave = 2;
$stmt = $connection->prepare("UPDATE competition_queue SET competition_queue_status = ? WHERE user_id = ? AND competition_queue_status = ?");
$stmt->bind_param("sss", $status_leave, $user_id, $status_queue);
$stmt->execute();
$stmt->close();
}
}
?>
Current result:
Expected result:
The for loop works as it should and you can check that by placing an var_dump($payer) inside the for loop;
The increment in the for loop is given by the third statement, the $player++, which means $player = $player + 1;
If you would want to have a different looping step, you can add a different expression for the third statement in the for loop.
e.g.
for($player = 0; $player <= 9; $player++) {
var_dump($player);
}
exit;
The code snippet will output 0 1 2 3 4 5 6 7 8 9
for($player = 0; $player <= 9; $player += 2) {
var_dump($player);
}
exit;
The code snippet will output 0 2 4 6 8
I hope this helped you better understand the for loop.
The actual problem you are facing is created by the logic that you have made with SQL and not by the for loop
Related
This question already has answers here:
How can I bind an array of strings with a mysqli prepared statement?
(7 answers)
Closed 1 year ago.
I am trying to sum a colomn based on the IDs selected from a table that i put in a array. For some reasom only the first ID is used in the Where clausule. When I echo the variable all the ids are there. What am i doing wrong?
$counttheid = array();
$stmt3 = $mysqli->prepare("SELECT
id
FROM account
WHERE level <= '5' AND door = ? AND `group_name` = ? AND betaald = 'Yes'");
$stmt3->bind_param("ss",$usernamesession,$groupname);
$stmt3->execute();
$result3 = $stmt3->get_result(); //only works when nd_mysli is set on the server!
while ($rowid = $result3->fetch_assoc())
{
$counttheid[] = $rowid['id'];
$countid = implode(',', $counttheid); // contains all the ids !!
}
$sql = "SELECT SUM(mobcash) AS totalcash FROM account WHERE id IN (?)
";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("i",$countid);
$stmt->execute();
$stmt->bind_result($row['totalcash']);
while($stmt->fetch()) $sumcash = $row['totalcash'];
echo $sumcash; // Somhow only the sum of the first ID of the array !!
echo $countid;// all the ids from the array !!
Not only for the in, but the number of bind parameters will need to match as well.
Try with this example for the code from the while to the execute:
while ($rowid = $result3->fetch_assoc())
{
$counttheid[] = $rowid['id'];
// $countid = implode(',', $counttheid); // contains all the ids !!
}
$in = str_repeat('?,', count($counttheid) - 1) . '?';
$types = str_repeat('i', count($counttheid));
$sql = "SELECT SUM(mobcash) AS totalcash FROM account WHERE id IN ($in)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param($types, ...$counttheid);
$stmt->execute();
At the bind_param, the part with ...$counttheid, the ... portion is the argument unpacking operator.
I have struggled with this concept for a while and am hoping someone can help, please. I can query database records if all values are stored in one row. The challenge I have is when the quantity values are stored on multiple rows but with a common key (e.g. order_id). I want to store into an array and then assign each value to an on-screen variable to allow user updates so that I can update the table again.
For example, if the table looks as follows:
id line part qty
-- ---- ---- ---
1 1 63 2
1 2 104 3
1 3 54 2
1 4 50 1
I have not had success with the following where I establish the number of rows and then try to build a foreach loop to capture the data:
$sql = 'SELECT *, COUNT(*) as $count FROM Order_Items where order_id = ?';
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
for ($x = 0; $x <= $count; $x++ {
$var($x) = $data['qty'];
}
If I can properly separate the values into an array and then reference them somehow, then I could do the following and get them displayed and easily update back to the database:
$var_1 = $data['63']; // part_id = 63
$var_2 = $data['104']; // part_id = 104
$var_3 = $data['54']; // part_id = 54
$var_4 = $data['50']; // part_id = 50
Change this:
$sql = 'SELECT *, COUNT(*) as $count FROM Order_Items where order_id = ?';
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
for ($x = 0; $x <= $count; $x++ {
$var($x) = $data['qty'];
}
To something that makes sense like this
$sql = 'SELECT * FROM Order_Items where order_id = ?';
$q = $pdo->prepare($sql);
$q->execute(array($id));
while(false !== ($row = $q->fetch(PDO::FETCH_ASSOC))){
echo $row['qty'];
};
UPDATE
$sql = 'SELECT * FROM Order_Items where order_id = ?';
$q = $pdo->prepare($sql);
$q->execute(array($id));
$array = [];
while(false !== ($row = $q->fetch(PDO::FETCH_ASSOC))){
$array['line_'.$row['line']] = $row;
};
UPDATE
The additional code that answers what I was looking for is as follows. Your answer was key to my understanding it all:
$sql = 'SELECT * FROM Order_Items where order_id = ?';
$q = $pdo->prepare($sql);
$q->execute(array($id));
$array = [];
while(false !== ($row = $q->fetch(PDO::FETCH_ASSOC))){
$qty['line_'.$row['line']] = $row['qty'];
};
$var_54 = $qty['line_54'];
$var_63 = $qty['line_63'];
I have an array in variable $id = Array([0] => 5 , [1]=> 6). Now I want to pass the value to the SQL query and calculate the SUM, but somehow it's not calculating the SUM rather then its displaying output 100500 which should be 600 (like: 100+500 = 600).
My PHP code is :
$id = $_POST['id'];
for($i = 0; $i<count($id); $i++) {
$sql = getAmount($id[$i]);
}
function getAmount(&$id){
global $mysqli;
$stmt = $mysqli->prepare("SELECT SUM(amount) AS total FROM work WHERE id = (?)");
$stmt->bind_param("s",$id);
$stmt->execute();
$stmt->bind_result($amount);
$stmt->fetch();
echo $amount['total'];
}
You are not summing the sql result;
$id = $_POST['id'];
$sum = 0;
for($i = 0; $i<count($id); $i++) {
$sum = $sum + getAmount($id[$i]);
}
echo $sum;//Print the summing result
function getAmount(&$id){
global $mysqli;
$stmt = $mysqli->prepare("SELECT SUM(amount) AS total FROM work WHERE id = (?)");
$stmt->bind_param("s",$id);
$stmt->execute();
$stmt->bind_result($amount);
$stmt->fetch();
return $amount['total'];
}
You should return query sum result from getAmount function and you should sum the returning results in the loop. After completed the loop, you can print the sum result.
column amount must be some of integer or float types: int, bigint, tinyint, longint, float, double
I've been working on a SQL statement that checks the database to see if feedback has been left for a specific qr_id from a specific uuid within 24 hours. The original code works as intended, but obviously to be more secure I wanted to change the code to a structed SQL statement instead.
$uuid = "DB8962A3-BC7A-481F-9D7E-C1FC7F74E50E";
$qrid = "2147483647";
$query = sprintf("SELECT COUNT(*) FROM `feedback` WHERE uuid = '%s' AND qr_id = '%s' AND created_on > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))", mysql_real_escape_string($uuid), $qrid);
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$size = $row['COUNT(*)'];
if ($size > 0){
echo 'Unable to leave feedback as user has left feedback for this employee within 24 hours.';
}else{
echo 'User has not left feedback for this employee within 24 hours, continue.';
}
The above works perfectly fine, if the count is above 0 then it stops the user posting. Here's my attempt at converting it to a structured SQL statement:
$stmt = $this->conn->prepare("SELECT COUNT(*) FROM `feedback` WHERE uuid = ? AND qr_id = ? AND created_on > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))");
$stmt->bind_param('si', $uuid, $qrID);
$result = $stmt->execute();
$size = $result['COUNT(*)'];
$stmt->close();
if ($size = 0){
// do post, return true
return true;
}else{
return false;
// do not post, return false
}
I have tried printing the result value and it's always 1, and trying to echo the $size variable returns nothing.
You need to fetch the result, try this:
$stmt->bind_param('si', $uuid, $qrID);
$stmt->execute();
//here you bind result to variable $size
$stmt->bind_result($size);
$stmt->fetch()
$stmt->close();
if ($size == 0){
// do post, return true
return true;
}else{
return false;
// do not post, return false
}
how do you select each day records for the last 7 days? if the dates are not available set it to 0. I tried the below but I am not getting any results.
$stmt = $db->prepare("SELECT DATE(order_date),COUNT(id) as 'total' FROM `orders` WHERE `order_date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()GROUP BY DATE(order_date)");
$stmt->execute();
$result = $stmt->get_result();
$total = [];
while($row = $result->fetch_object()){
$total[] = $row->total;
}
print_r($total);
not the actual solution I wanted but I managed to get the results by wrapping the query in a function and calling it 7 times...
function weeklyData($day){
$stmt = $db->prepare("SELECT order_date from orders where DATE(FROM_UNIXTIME(order_date)) = CURDATE() - INTERVAL ? DAY");
$stmt->bind_param('s', $day);
$stmt->execute();
$stmt->store_result();
return $stmt->num_rows;
}
$total = [];
for($x = 1; $x <= 7; $x++){
$total[] = weeklyData($x);
}
print_r($total);