PHP MYSQL link array with uuid Json - php

First of all, I'm happy to see people helping people here :D.
I got also a small issue sadly,
DB 1 contains:
UUID and much values.
DB 2 contains:
UUID and again many other values.
what I want is
[0] => Array
(
[UUID] => 96
[DB1 values]
[DB2 values]
)
i thought that will work with array_combine but that isn't true sadly because then i miss some db1 values
here database images:

--EDIT--
Your picture helped. You can handle that at the SQL Level
SELECT Login.*, Location.*
FROM Login
LEFT JOIN Location ON Login.UUID=Location.UUID
GROUP BY Login.UUID
Then the resulting data in PHP will already be correctly formatted.
Notice that I chose table Login to drive the query, could have also chosen the Location table. depends on the underlying structure

Related

save formula in database and substitue value at runtime php

i want to store the following formula in database
Z-Score = 1.2(Working_Capital/Total_Assets) + 1.4(Market_Value/Total_Assets)
Working_Capital,Total_Assets,Retained_Earnings are stored in database against different tables
eg:- Total_Assets = BalanceSheet.totalAssets, Market_Value = expert.market_value
this formula can be changed by the user through a form, he can add more quantities to the formula
Z-Score = 1.2(Working_Capital/Total_Assets) + 1.4(Market_Value/Total_Assets)+1.0(Sales/Total Assets)
PROBLEM:-
now keeping in mind that the formula can change anytime i need to store this in the database accross the user id so that i can give it an edit option.
also the formula is used to calculate data over period of years.
many users will have many formula.
APPROACH:-
from what i read so far many suggest to store it as a string, it is not possible since the values i need to display the user for editing the formula is different than what it is in database.
some suggest to convert the expression to tree data structure but i don't know which tree structure will suit my application???
lastly i need to store it in mysql database so will tree storing and retrieving be faster???
i am asking for suggestions as i feel its a complex problem but if anyone have already solved it PHP then please feel free to share
thanks in advance.
Solution to the above approach that i came up with.
1) take the formula from the user and when he is selecting the formula operands create a new array of operands which maps the names of operands in frontend with they corresponding values of database table and colunm name
eg:-
`$input['operands'] = array(
array('Total_Assets' => 'balance_sheet.TotalAssets'),
array('Working_Capital' => 'balance_sheet.WorkingCapital'),
array('Revenue' => 'balance_sheet.TotalRevenue'),
);`
2) create an infix form of the formula from which you can create a binary tree of the following form
infix form:-
(
[0] => revenue
[1] => total_assets
[2] => working_capital
[3] => +
[4] => /
)
Binay tree:-
{"__exp1":{"operator":"+","operand":["working_capital","total_assets"]},"root":{"operator":"\/","operand":["__exp1","revenue"]}}
3) now send all this to the server and there i save it as each column of table, also reverse mapping of the tree to infix form of the formula is possible on server side
4) this helps me to substitute the database values since i have the database column array from which i get the values and execute the formula and return the values.

How to optimize a slow MySQL Query (via PHP)

I am attempting to run the following mySQL query via a piece of PHP code on the front end of a website. For some reason when I run this on my AWS instance the performance is abysmal. However, when I run it localhost (wamp) I have no problems at all. It will even return results for larger data sets. I am somewhat of a novice when it comes to mySQL. Thoughts?
SELECT *
FROM bigfoot
WHERE family_id IN
(
SELECT family_id AS family_id
FROM bigfoot
WHERE user_email = '$email'
GROUP BY family_id
)
As you can see, the primary element in gathering specific row data for this query is family_id. However, only the first row of each family_id contains an email address of which in this case is used as the basis for the query. I have searched various posts in the form, but am still unsure of how to move forward.
Thanks in advance,
Randy
I will go out on a limb, why not.
I think this would be blazing fast:
select b1.*
from bigfoot b1
join bigfoot b2
on b2.family_id=b1.family_id
and b2.user_email='$email'
assuming you had a composite index on bigfoot (family_id,email) ... a big assumption. plus an index on email
It does show however how to lose the correlated subquery.
b2 would be isolated to email, getting family. b1 will be all for that family, if I did it right

MYSQL PHP jump through many tables by one query

Is it possible to get data from one table when necessary data is in the other one(and so on a few times)? I mean, I have to get result, fetch array, pull all the data in php, make another query... and repeat it a several times before I get the wanted result. Could it be done in one query or have you guys got any ideas about optimization for such issue? I've been searching, but I really didn't find anything what makes sense for me.
UPDATE:
Thanks! JOIN fixed my problems but I got some more to work on. Let's say I've got tables like this
Users:
ID Name
1 Adam
2 John
3 Lana
Roles(with users ids)
ID Name Cleaner Soldier Doctor
1 Ship crew 2 1 3
How can I get my result like:
[1,Ship crew,John,Adam,Lana] in php without making many queries? I mean I'd like to load records from another table for a several fields depending on primary key ID.
EDIT:
OK I got it, I just needed some practice with mysql. It's not so hard as I thought it could be. Thanks for that join now I know what I was looking for :)
It is possible yes, you can use the "JOIN" technique to collate the tables or you can use the "IN" clause in your query .
http://www.tutorialspoint.com/mysql/mysql-in-clause.htm

php checkbox update, insert, delete mysql records

I've searched quite a bit for this but nothing has seem to to come up which is strange because I think it would be something that's something quite easy to implement. Basically I have a list of items which the user selects, what I want to do is update the values in the database according to the checkboxes selected basically the following scenarios:
The id assigned in the checkbox array is already in the DB and nothing needs to happen, or can be updated with the same value.
The id assigned in the checkbox array needs to be added to the database.
The id ISN't assigned in the checkbox array and therefor must be deleted from the DB.
has anyone got any code they worked with that does this?
EDIT:
the table is simple with two things being updated:
id_text and id_product
array coming from $_POST is:
Array ( [text] =>
Array ( [0] => 8 [1] => 1 [2] => 2 ) // the values that need to be inserted, updated, or deleted if they don't exist here.
[textValue] => ALL OF THEM
[id] => 33 // the 'id_product value'
[update] => update )
True, without any code or database squema is pretty hard to help, but if I understand correctly you can just:
Delete all values on DB.
Loop through the ids passed in POST or GET and insert them on DB.
Do an replace for every id passed. Replace behaves like insert i the entry does not exist and like update if it exists. I can not give you more details becuase I do not know your table structure
Delete all items that have a id that is not in your array. you can select those with "in":
...WHERE id_product NOT IN (1,3,5,8) AND......
you can easily generate this by using
$idCondition = 'id_product NOT IN (' . implode(',',$yourArrayName) . ')';
About REPLACE:
http://dev.mysql.com/doc/refman/5.1/de/replace.html
About IN:
http://www.webdevelopersnotes.com/tutorials/sql/tutorial_mysql_in_and_between.php3
EDIT: I just realized that some people might have objections on my deinition of REPLACE. What REPLACE actually does is deleting the row if it already exists and then do an INSERT.

PHP/MYSQL problem with BIGINT

I have a mysql database and a table to store stuff from twitter API. When i parse data with PHP from twitter i use mysql_query to insert data into the table.
I have a weird problem with the ID of the tweets:
For example the status update with ID 15861323074113537 (a tweet from google) is stored in the database as: 15861323074114000 (the last 4 digits are altered).
The php query is:
$sql = mysql_query("INSERT INTO $table (id,tw_text) VALUES ($id,'$tw_text')");
If i edit the record via phpmyadmin the correct value is stored (15861323074113537). The column is BIGINT.
So i guess something weird is going on with the php function mysql_query and the INSERT command.
Any ideas/solutions?
Thanks in advance
ok found the problem, it wasn't code related. Twitter returns 2 IDs and one of those has 000 in the end. [id_str] => 15861323074113537 and [id] => 15861323074114000
Damn twitter API!
Sorry for the trouble :/
PHP integers cannot be that large. When a number that doesn't fit into an integer is used, it is converted to a float. What you are probably seeing is that even the float is not precise enough to contain that many digits. Follow webbiedave's advice and keep it as a string to avoid rounding.

Categories