PHP calculation different with SQL & Excel result - php

I tried to calculate using SQL and found out that the result was different with the excel one,
My table setting :
+--------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+---------------+------+-----+---------+----------------+
| qty_final | int(11) | NO | | NULL | |
| price_final_usd | decimal(20,2) | NO | | NULL | |
+--------------------+---------------+------+-----+---------+----------------+
My table content:
+-----------+-----------------+
| qty_final | price_final_usd |
+-----------+-----------------+
| 1 | 53.22 |
| 2 | 125.05 |
| 3 | 23.15 |
| 4 | 114.79 |
| 4 | 395.06 |
+-----------+-----------------+
my sql query :
UPDATE $table SET amount_final = round((qty_final * price_final_usd),2)
my php code :
$amount_final = round($qty_final * $price_final_usd, 2);
my excel formula :
=ROUND(qty_final * price_final_usd,2)
Here's the result comparison
Can someone tell me how to correct the PHP code ?
I want exactly the same result between them

Related

Sort by reference count php

How can I sort by reference count?
In the above picture,
2-1
1-1
I want to go out like this.
I'm sorry for my bad english
To sort a result set by a column you would append an ORDER clause to your query.
For example:
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
| Buffy | 1989-05-13 |
| Bowser | 1989-08-31 |
| Fang | 1990-08-27 |
| Fluffy | 1993-02-04 |
| Claws | 1994-03-17 |
| Slim | 1996-04-29 |
| Whistler | 1997-12-09 |
| Chirpy | 1998-09-11 |
| Puffball | 1999-03-30 |
+----------+------------+
More information can be found in the manual:
https://dev.mysql.com/doc/refman/5.7/en/sorting-rows.html

multiple or single selection

i have two tables and column name are as :
Table 1
user | food | color | bike | car
Table 2
user | mobile | laptop
Now i want to get result by select single or multiple value.
For example, if i want select user which have bike and laptop . then i can get result it by query but for this all fields i have to use many condition . i have used if else where. and i also want to refine select with current selection . so what should i use ? Please help my previous question was same but i did not asked perfectly. so asked again. Thank You.
You can use multiple tables in your single SQL query. The act of joining in MySQL refers to smashing two or more tables into a single table.
You can use JOINS in SELECT, UPDATE and DELETE statements to join MySQL tables.
Example
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran | 20 |
| mahnaz | NULL |
| Jen | NULL |
| Gill | 20 |
| John Poul | 1 |
| Sanjay | 1 |
+-----------------+----------------+
SELECT * from tutorials_tbl;
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
| 1 | Learn PHP | John Poul | 2007-05-24 |
| 2 | Learn MySQL | Abdul S | 2007-05-24 |
| 3 | JAVA Tutorial | Sanjay | 2007-05-06 |
+-------------+----------------+-----------------+-----------------+
SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
-> FROM tutorials_tbl a, tcount_tbl b
-> WHERE a.tutorial_author = b.tutorial_author;
+-------------+-----------------+----------------+
| tutorial_id | tutorial_author | tutorial_count |
+-------------+-----------------+----------------+
| 1 | John Poul | 1 |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
http://www.tutorialspoint.com/mysql/mysql-using-joins.htm

Codeigniter join multiple tables with where clauses

I have four tables that I need to join together, with one of those tables having two different records I need to grab. The guild and party tables I'm getting the name of those so that I can display the name instead of ID to the user, and the charlog table has the char_id's creation date and last login date that I want to show as well. Here are the tables:
char table:
----------------------------------------------------
| char_id | name | guild_id | party_id |
|---------------------------------------------------
| 150000 | char1 | 3 | 3 |
| 150001 | char2 | 2 | (NULL) |
| 150002 | char3 | (NULL) | 1 |
| 150003 | char4 | 1 | 2 |
----------------------------------------------------
guild table:
-------------------------
| guild_id | name |
-------------------------
| 1 | guild_1 |
| 2 | guild_2 |
| 3 | guild_3 |
-------------------------
party table:
-------------------------
| party_id | name |
-------------------------
| 1 | party_1 |
| 2 | party_2 |
| 3 | party_3 |
-------------------------
charlog table:
----------------------------------------------------
| time | char_id | char_msg |
----------------------------------------------------
| 2015-02-14 06:45:32 | 150000 | make new char |
| 2015-02-14 06:45:58 | 150000 | char select |
| 2015-02-15 12:32:19 | 150001 | make new char |
| 2015-02-15 16:54:01 | 150000 | char select |
| 2015-02-15 19:23:54 | 150001 | char select |
| 2015-02-16 01:32:13 | 150002 | make new char |
| 2015-02-16 01:33:01 | 150003 | make new char |
| 2015-02-16 04:45:43 | 150000 | char select |
| 2015-02-16 07:43:22 | 150003 | char select |
----------------------------------------------------
As mentioned, I need to get the make new char entry from the charlog table to display when the character was created and as well the LAST (by date) char select entry to display when the character was played, all of this with one single char_id.
All in all, I'd be looking for a table that looks like this:
---------------------------------------------------------------------------------------------------------------------
| char_id | name | guild_id | guild_name | party_id | party_name | create_time | lastlogin_time |
---------------------------------------------------------------------------------------------------------------------
| 150000 | char1 | 3 | guild_3 | 3 | party_3 | 2015-02-14 06:45:32 | 2015-02-16 04:45:43 |
I'm using the following active record lines in Codeigniter to attempt to get the data I need. It returns the create_date correctly but the lastlogin_time is not returned (it's blank):
function get_char_info($cid) {
$this->db->select('char.*,guild.guild_id,guild.name AS guild_name,party.party_id,party.name AS party_name,charlog1.time AS create_time,charlog2.time AS lastlogin_time');
$this->db->from('char');
$this->db->where('char.char_id', $cid);
$this->db->join('guild', 'char.guild_id = guild.guild_id', 'left');
$this->db->join('party', 'char.party_id = party.party_id', 'left');
$this->db->join('charlog AS charlog1', 'char.char_id = charlog1.char_id AND charlog1.char_msg = "make new char"', 'left');
$this->db->join('charlog AS charlog2', 'char.char_id = charlog2.char_id AND charlog2.char_msg = (SELECT max(charlog.time) FROM charlog WHERE char_msg = "char select")', 'left');
$query = $this->db->get();
return $query->row();
}
As mentioned, the guild_name, party_name and create_time come through correctly, but the lastlogin_time is blank, no error.
I've tried jumbling some things around in the active record clauses but can't get the lastlogin_time to show. Any help would be appreciated.
The join on your subquery is not correct :
It should be charlog2.time = (SELECT....) not charlog2.char_msg = (SELECT....)

Get all dbpprops from a certain box in wikipedia

Let me use these 2 links as my examples:
http://en.wikipedia.org/wiki/Maarten_Stekelenburg
http://dbpedia.org/page/Maarten_Stekelenburg
What I want to do is get all the dbpprops which are in the top right infobox (the one with the picture).
Here I encounter 2 problems:
How do I get all the properties without knowing them (We are putting together a football player database, and the prop names are all different for every player)
How do I make sure those are the properties that are inside the box, because there are many other dbpprops on that page and it doesn't look like dbpedia differentiates between those.
After that I want to add all those properties to an array(if that is possible).
If it's possible, I would like it to look something like this:
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX dbp2: <http://dbpedia.org/ontology/>
PREFIX dbp3: <http://dbpedia.org/property/>
SELECT *
WHERE {
dbp:".$term." dbp2:abstract ?abstract .
dbp:".$term." dbp2:thumbnail ?img .
dbp:".$term." dbp3:* ?properties . //This would then be replaced by the necessary line
FILTER langMatches(lang(?abstract), 'en')
}
EDIT: I am working with PHP
I can't guarantee that the set of properties from the infobox will be exactly the properties that are dbpprop properties (as opposed to dbpedia-owl properties), but it looks like there's a fairly good correspondence. In this case, you could use a query like the following which asks for properties and values of the person you mentioned, but only thoe properties that are in the dbpprop namespace.
prefix dbpedia: <http://dbpedia.org/resource/>
prefix dbpprop: <http://dbpedia.org/property/>
select ?property ?value where {
dbpedia:Maarten_Stekelenburg ?property ?value
filter( strstarts(str(?property),str(dbpprop:)) )
}
SPARQL results
----------------------------------------------------------------------------------------------------------------------
| property | value |
======================================================================================================================
| dbpprop:bg | "gold"#en |
| dbpprop:bg | "#F1771D"#en |
| dbpprop:birthDate | "1982-09-21+02:00"^^<http://www.w3.org/2001/XMLSchema#date> |
| dbpprop:birthPlace | "Haarlem, Netherlands"#en |
| dbpprop:caps | 44 |
| dbpprop:caps | 191 |
| dbpprop:clubnumber | 24 |
| dbpprop:clubs | dbpedia:A.S._Roma |
| dbpprop:clubs | dbpedia:AFC_Ajax |
| dbpprop:currentclub | dbpedia:A.S._Roma |
| dbpprop:dateOfBirth | 22 |
| dbpprop:fg | "navy"#en |
| dbpprop:fg | "white"#en |
| dbpprop:fullname | "Maarten Stekelenburg"#en |
| dbpprop:goals | 0 |
| dbpprop:name | "Maarten Stekelenburg"#en |
| dbpprop:name | "Stekelenburg, Maarten"#en |
| dbpprop:nationalcaps | 4 |
| dbpprop:nationalcaps | 54 |
| dbpprop:nationalgoals | 0 |
| dbpprop:nationalteam | dbpedia:Netherlands_national_football_team |
| dbpprop:nationalteam | dbpedia:Netherlands_national_under-21_football_team |
| dbpprop:nationalyears | 2002 |
| dbpprop:nationalyears | 2004 |
| dbpprop:ntupdate | 18 |
| dbpprop:pcupdate | 21 |
| dbpprop:placeOfBirth | "Haarlem, Netherlands"#en |
| dbpprop:position | <http://dbpedia.org/resource/Goalkeeper_(association_football)> |
| dbpprop:shortDescription | "Dutch footballer"#en |
| dbpprop:title | "Awards"#en |
| dbpprop:title | "Netherlands squads"#en |
| dbpprop:years | 2002 |
| dbpprop:years | 2011 |
| dbpprop:youthclubs | dbpedia:AFC_Ajax |
| dbpprop:youthclubs | "Schoten"#en |
| dbpprop:youthclubs | "Zandvoort '75"#en |
| dbpprop:youthyears | 1997 |
| dbpprop:wordnet_type | <http://www.w3.org/2006/03/wn/wn20/instances/synset-soccer_player-noun-1> |
| dbpprop:hasPhotoCollection | <http://wifo5-03.informatik.uni-mannheim.de/flickrwrappr/photos/Maarten_Stekelenburg> |
----------------------------------------------------------------------------------------------------------------------
You mentioned storing results in an array, but I don't really understand the example that you've provided. It looks more like a query than code to put something into an array. Additionally, you haven't specified what programming language you're working with, so we can't really respond with array manipulation code.

My table in postgresql is truncating these strings

I have a table in VMobjects like this
MGRCONFIG_DB=# select * from vmobjects;
guid | ipaddress | username | password | hostid | vmname | guestostype | guestos
name
-----------------------------------+---------------+----------+----------+----------+-----------------+-------------+--------
-----
7728235734dcd25700e7c02.96324791 | gsdag | gsdasd | | Physical | rag | windows |
3642656604dcd343d3bcd11.54875889 | fsd | | | Physical | derde | windows |
17714467484dcd35dd0fa677.27764184 | dsf | | | Physical | fdsfd | windows |
1837080764dcd362fafe404.83675706 | fgf | | | Physical | fgf | windows |
2791118544dcd363df11bf1.21924610 | fdghhg | | | Physical | $%^ | windows |
7716352574dcd365c9eb777.30236042 | dsffd | | | Physical | ^ | windows |
10753160514dcd366631f5b6.48505657 | gfdgd | | | Physical | # | windows |
8253046164dcd366f177bc3.85542378 | ghgfdhg | | | Physical | ############## | windows |
9126180214dcd367a5b42e0.23605256 | fsdfdsfdsf | | | Physical | fdsaj;( | windows |
11086632854dcd36f62f7e79.14470771 | dfsdfsd | | | Physical | ^ | windows |
Now I have a php page addvm.php, when I add username/ip/password/ or anything it gets truncated
gets truncated on entering data as '~!##$%^&*()_+=-`{}|\][:"';<>?/.,' for all fields.
After using pg_escape_string
i am able to insert '~!##$%^()_=-`{}|][:"';<>?/. all strings except + and &.
#Emil Vikström: say that i have to use urlencode for this. but i don't no, How & whr it is used?
Use pg_escape_string on the data before entering it into your SQL query:
$data = '~!##$%^&*()_+=-';
$data_escaped = pg_escape_string($data);
$query = 'INSERT INTO table (data) VALUES("'. $data_escaped .'");';

Categories