MariabDB synchronous UPDATE query - php

I'm working with PHP and MariaDB and I run into a problem.
I update a value to multiple rows, and then SELECT there rows to make a new calculation the data for another task.
The problem here that I get the wrong number. I guess that the MariaDB has not finished the UPDATE query, but it return the finished flag to PHP and then the PHP proceeds the SELECT query. [I just guess]
I open to any idea. If I'm wrong, please correct me.
Thank you for sharing
This is my code
$modelAdminOrderBidSys = $this->load->model('Admin\Order\BidSys');
$acceptedItem = typeCast($modelAdminOrderBidSys->getItem($cartItemId));
if (!$acceptedItem) {
return array(
'result' => 'error',
'message' => 'Cannot find item #' . $cartItemId
);
}
$acceptedItem['lastOffer'] = $acceptedItem['offer'];
$acceptedItem['accepted'] = 1;
$acceptedItem['isBot'] = 0;
$modelAdminOrderBidSys->updateItem($cartItemId, array2object($acceptedItem));
$cartItems = typeCast($modelAdminOrderBidSys->getItems($acceptedItem['cartId']));
$accepted = 1;
$total = 0;
$offer = 0;
$lastOffer = 0;
foreach($cartItems as $cartItem) {
if ((int)$cartItem['accepted'] < 1) {
$accepted = 0;
}
$total += (float)$cartItem['total'];
$offer += (float)$cartItem['offer'];
$lastOffer += (float)$cartItem['lastOffer'];
}
$postField = new \stdClass();
$postField->accepted = $accepted;
$postField->total = $total;
$postField->offer = $offer;
$postField->lastOffer = $lastOffer;
$modelAdminOrderBidSys->updateCart($acceptedItem['cartId'], $postField);

It sounds like your SELECT transaction starts before the UPDATE has committed. Try changing the transaction_isolation (in config) / tx_isolation (at runtime with SET GLOBAL) to READ-COMMITTED. Default is REPEATABLE-READ.

Related

How to rewrite a php script to JavaScript

I have an express server running using nodejs, and I'm using SQL syntax to query data from my MYSQL database.
The query is to get all members under the current user ID and also get members under it's children and so on, to get a genealogy tree. I'm trying to query a genealogy tree for a user.
I have a PHP script for the query, which worked fine
This is the PHP script:
$memory = $_GET['memory'];
echo '<div style="background:#ccc;display:flex;width:50px;height:50px;justify-
content:center;align-items:center;border-radius:50%;">'.$memory.'</div>';
$transend = array($memory);
$step = 1;
for ($i=0; $i < $step; $i++) {
$under = $transend[$i];
$get = $con->query("SELECT * FROM multilevel WHERE `under`='$under'");
$cnt = $get->num_rows;
if ($cnt>0) {
for ($g=0; $g < $cnt; $g++) {
$fet = $get->fetch_object();
$id = $fet->id;
$position = $fet->position;
array_push($transend, $id);
$step = $step*2;
echo '<div style="background:#ccc;display:flex;width:50px;height:50px;justify-content:center;align-items:center;border-radius:50%;">'.$id.'-'.$position.'</div>';
}
} else {
break;
}
}
I am trying to rewrite it using SQL syntax in node. This is my nodejs code:
A Tree Constructor
const Tree = function (binary) {
this.position = binary.position;
this.level = binary.level;
this.user_id = binary.user_id;
this.under_user_id = binary.under_user_id;
this.brought_by = binary.brought_by;
this.username = binary.username;
};
A method to get all binary for a user
Tree.findBinaries = async (id, result) => {
const theId = parseInt(id);
let transend = [theId];
let step = 1;
let data = [];
for (let i = 0; i < step; i++) {
const under = transend[i];
connection.query(
`SELECT * FROM multilevel WHERE under = ?`,
under,
(err, res) => {
if (err) {
result(null, err);
return;
}
if (res.length) {
for (let index = 0; index < res.length; index++) {
const { id } = res[index];
// push id
transend.push(id);
data.push(res[index]);
step = step * 2;
}
result(null, data);
} else {
result(null, "no data found");
}
}
);
}
};
The transend.push(id) and data.push(res[index]), are not been pushed to the top level, also the step is not been updated. So the loop is running just once.
The expected Result is supposed to be 16 items of children and children of children, But it is currently returning to just the first level. I don't know what I'm currently doing wrong.

Fetching latest value from database even when there are no rows present

So I'm trying to fetch a points table for users to add points in by garnering their total points and adding it with the installation points, however by trying to fetch their "latest" points, new users who do not have an existing row in the table will throwback an error "InvalidArgumentException; Data Missing". This would be my code below, and are there any other ways around this?
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
$points['points_total'] = $currentpoint + $battery['point_installation'];
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first(); of your code return an object and you are try to perform math (addition) operation on that object which is not possible. You can update your code like below.
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
if($currentpoint != null)
{
$points['points_total'] = $currentpoint->points_total + $battery['point_installation'];
}
else {
$points['points_total'] = $battery['point_installation'];
}
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);

is there any possible chance when i input data into loop and doesnt entered into database?

sorry for my bad english . i got problem with looping . my friend said when he used my program and he insert 20 data or more into database in one time , sometimes a data doesnt match with data previously entered .
an example : he insert 20 data and he just got 19 but the problem is that happening sometimes.
so im confused now , how can the program error just sometimes ?
here my code :
else if ($mod == 'suratkeluar'){
$batas = $_GET['batas'];
if (isset($_POST['submit'])) {
for ($i = 0; $i < $batas; $i++) {
$idlot = $_POST['hiddenlot'][$i];
$idbenang = $_POST['hiddenbenang'][$i];
$benang = $_POST['jenisbenang'][$i];
$warna = $_POST['warna'][$i];
$lot = $_POST['lot'][$i];
$harga = $_POST['hiddenprice'][$i];
$netto = $_POST['netto'][$i];
$box = $_POST['box'][$i];
$cones = $_POST['cones'][$i];
$ket = $_POST['keterangan'][$i];
$cussplit = explode('_',$_POST['customer']);
$idcus = $cussplit[0];
$cus = $cussplit[1];
$alamatcus = $cussplit[2];
$kota = $cussplit[3];
$POsplit = explode('_',$_POST['nopo']);
$idpo=$POsplit[0];
$nopo=$POsplit[1];
$mobilsplit = explode('_',$_POST['kendaraan']);
$kendaraan = $mobilsplit[0];
$plat = $mobilsplit[1];
$identitas = $_POST['identitas'][$i];
$month = date('n');
$years=date('Y');
if($idlot != 0){
$a=mysql_query("INSERT INTO surat_jalan VALUES ('',now(),'$_POST[nosurat]','$idlot','$benang','$warna','$lot','$harga','$netto','$box','$cones','$idcus','$cus','$alamatcus','$kota','$idpo','$nopo','$ket',1,'$month$years','$identitas','$kendaraan','$plat','$idbenang')");
$aax=mysql_query("SELECT * FROM lot WHERE id_lot='$idlot'");
$xx=mysql_fetch_array($aax);
$net=$xx['netto'] - $netto;
$bbox=$xx['box'] - $box;
$ccones=$xx['cones'] - $cones;
if($net == 0)
{
$a= mysql_query("UPDATE lot SET netto = '$net',
box = '$bbox',
cones = '$ccones',
warning = 1
WHERE id_lot = '$idlot'");
}
else
{
$a=mysql_query("UPDATE lot SET netto = '$net',
box = '$bbox',
cones = '$ccones'
WHERE id_lot = '$idlot'");
}
}
$b = $i + 1;
for ($c = 0; $c < $box; $c++) {
$packinglist = $_POST["packinglist$b"][$c];
$a= mysql_query("INSERT INTO packing_list VALUES('','$packinglist','$surat[id_surat]','$surat[no_surat_jalan]')");
}
}
}
}
How about this? I'll give an example but this could go with (probably) all queries. Let's say, this one (second from the last):
$a = mysql_query("UPDATE lot SET netto = '$net', box = '$bbox', cones = '$ccones' WHERE id_lot = '$idlot'");
To check if it was successful, you can add additional lines:
if ($a) // Success?
print("Row has been updated.<br>");
else // Error
print("An error has occured; row has not been updated. Reason: ".mysql_error()."<br>");
If there are no redirections at the end, etc. and there is at least one error (the last one?), then your friend will see something like this:
An error has occured; row has not been updated. Reason: #1054 - Unknown column 'cones' in 'where clause'
An error has occured; row has not been updated. Reason: #1054 - Unknown column 'box' in 'field list'
With that help, you can see if there is an error in your queries or in application itself (timeout?).
There is also another problem: you're using PHP's mysql_API, which is deprecated. Instead, you should upgrade and use mysqli_API.

Weighted Load Balancing Algorithm into PHP Application

I want to resolve weighted an Adapter from an factory which could be configured by user (enable/disable and weight %).
Example:
AdapterW ≃ 20% of transaction
AdapterX ≃ 30% of transaction
AdapterY ≃ 40% of transaction
AdapterZ ≃ 10% of transaction
I can grant that all items will never sum more than one hundred (100%), but sometimes any adapter could be deactivated.
I have the following parameters:
public function handleAdapter()
{
$isWActive = (boolean)$this->_config[self::W];
$isXActive = (boolean)$this->_config[self::X];
$isYActive = (boolean)$this->_config[self::Y];
$isZActive = (boolean)$this->_config[self::Z];
$WPercentage = (int)$this->_config[self::LOAD_BALANCE_W];
$XPercentage = (int)$this->_config[self::LOAD_BALANCE_X];
$YPercentage = (int)$this->_config[self::LOAD_BALANCE_Y];
$ZPercentage = (int)$this->_config[self::LOAD_BALANCE_Z];
.
.
.
return (self::W | self::X | self::Y | self::Z);
}
How can i balance weighted between this adapters dynamically?
Edit
created a gist to a executable code: https://gist.github.com/markomafs/5d892d06d6670909f9b4
This may not be the best approach, but you can try something like this:
public function handleAdapter()
{
//an array to return the balanced entries
$balancedEntries[] = false;
//verifies which of the options are active
$isWActive = (boolean)$this->_config[self::W];
$isXActive = (boolean)$this->_config[self::X];
$isYActive = (boolean)$this->_config[self::Y];
$isZActive = (boolean)$this->_config[self::Z];
//get configured percentage of each
$WPercentage = (int)$this->_config[self::LOAD_BALANCE_W];
$XPercentage = (int)$this->_config[self::LOAD_BALANCE_X];
$YPercentage = (int)$this->_config[self::LOAD_BALANCE_Y];
$ZPercentage = (int)$this->_config[self::LOAD_BALANCE_Z];
//here you fill the array according to the proportion defined by the percentages
if ($isWActive) {
for ($i = 0; $i < $WPercentage; $i++) {
$balancedEntries[] = self::W;
}
}
if ($isXActive) {
for ($i = 0; $i < $XPercentage; $i++) {
$balancedEntries[] = self::X;
}
}
if ($isYActive) {
for ($i = 0; $i < $YPercentage; $i++) {
$balancedEntries[] = self::Y;
}
}
if ($isZActive) {
for ($i = 0; $i < $ZPercentage; $i++) {
$balancedEntries[] = self::Z;
}
}
return $balancedEntries;
}
And then, in case you want a proportion of 1 to 100 (as in percentages):
$balancedResult = $balancedEntries[array_rand($balancedEntries, 1)];
Since array_rand will return 1 key from the original array, you use it to get it's value.
Another try, this should work for your case - But it only work if you have an adapter as a single char string, this is not visible by your question.
public function handleAdapter()
{
# a map with all adapters
$map = array(
self::W => self::LOAD_BALANCE_W,
self::X => self::LOAD_BALANCE_X,
self::Y => self::LOAD_BALANCE_Y,
self::Z => self::LOAD_BALANCE_Z
);
# generate a string map with one char per percentage point
$stringMap = "";
foreach($map as $key => $value){
# skip if disabled
if(!$this->_config[$key]) continue;
# repeat the key for each percentage point
$stringMap .= str_repeat($key, (int)$this->_config[$value]);
}
# return a random string char from the map
return $stringMap[rand(0, strlen($stringMap) - 1)];
}
Edit: I've misunderstood the question, the answer is wrong.
I understand your question so that you always want to return the adapter with the lowest load to force traffic to this adapter.
public function handleAdapter()
{
$isWActive = (boolean)$this->_config[self::W];
$isXActive = (boolean)$this->_config[self::X];
$isYActive = (boolean)$this->_config[self::Y];
$isZActive = (boolean)$this->_config[self::Z];
$WPercentage = (int)$this->_config[self::LOAD_BALANCE_W];
$XPercentage = (int)$this->_config[self::LOAD_BALANCE_X];
$YPercentage = (int)$this->_config[self::LOAD_BALANCE_Y];
$ZPercentage = (int)$this->_config[self::LOAD_BALANCE_Z];
$map = array();
if($isWActive) $map[self::W] = $WPercentage;
if($isXActive) $map[self::X] = $XPercentage;
if($isYActive) $map[self::Y] = $YPercentage;
if($isZActive) $map[self::Z] = $ZPercentage;
asort($map);
return key($map);
}
Edit: Fixed wrong sort(), you need asort() to maintain the index.

How do I delete course enrolments in moodle in bulk?

Moodle has a built in per course reset ability. I want to reset all my courses.
Sorry to bring this old post back but after a long struggle I finally got that code to work. I've tested it on a Moodle 1.9.7 enviroment
<?php
require('../config.php');
require_once('reset_form.php');
$courseids = array(8,9,11);
foreach ($courseids as &$value) {
$data->MAX_FILE_SIZE = 8097152;
$data->reset_start_date = 1251781200;
$data->reset_events = 1;
$data->reset_logs = 1;
$data->reset_notes = 1;
$data->reset_roles = Array(5);
$data->mform_showadvanced_last = 0;
$data->reset_roles_local = 1;
$data->reset_gradebook_grades = 1;
$data->reset_assignment_submissions = 1;
$data->reset_forum_all = 1;
$data->reset_quiz_attempts = 1;
$data->id = $value;
require_login($value);
require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $value));
$status = reset_course_userdata($data);
}
?>
Ok, I figued it out, but be very carful. If you do not know what you are doing do not use this code, you could do a lot of damage to your DB. This does the "default" course reset to all course ids in the array.
<?php
require('../config.php');
require_once('reset_form.php');
$courseids = array(8,9,11);
foreach ($courseids as &$value) {
$data->MAX_FILE_SIZE = 8097152;
$data->reset_start_date = 1251781200;
$data->reset_events = 1;
$data->reset_logs = 1;
$data->reset_notes = 1;
$data->reset_roles = Array(5);
$data->mform_showadvanced_last = 0;
$data->reset_roles_local = 1;
$data->reset_gradebook_grades = 1;
$data->reset_assignment_submissions = 1;
$data->reset_forum_all = 1;
$data->id = $value;
$status = reset_course_userdata($data);
}
?>
You should not reset courses this way, its too risky. Use the Course administration feature Choose the course, find the course admin. block, click the reset button, very simple. If you have only 3 courses, this takes about 30 seconds.

Categories