id | title | text
1 | aa |
2 | aa |
3 | aa |
I have some data from json data, i am not sure how many datas have duplicate in it.
$json = '[{"a":"1","b":"bb"},{"a":"2","b":"cc"},{"a":"3","b":"bb"}]';
$array = json_decode($json);
foreach($array as $key){
UPDATE table SET text = '".$key->b."' WHERE id = '".$key->a."' and title='aa'");
}
For example, as this situation, $key->b has 2 data bb from the json data, I only want update the first one and check if bb has already in the database, then ignore update.
id | title | text
1 | aa | bb
2 | aa | cc
3 | aa | <-ignore updtae, left the data empty
I know there have an easy way, first select * from table where text != '$key->a' for check, but this will cost 2 mysql query and make one more foreach, so
how to use one mysql query, update data without duplicate?
many thanks.
If your database is MySQL, maybe you can use the ON DUPLICATE KEY UPDATE.
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
I suggest using an array to store all the values of b you have used so far and only run the UPDATE if the value didn't exist yet.
$json = '[{"a":"1","b":"bb"},{"a":"2","b":"cc"},{"a":"3","b":"bb"}]';
$usedValues = array();
$array = json_decode($json);
foreach($array as $key){
if(!isset(usedValues[$key->b])) {
mysql_query("UPDATE table SET text = '".$key->b."' WHERE id = '".$key->a."' and title='aa'");
usedValues[$key->b] = true;
}
}
EDIT: If your database already has values for text this may still produce duplicates, but you could do a SELECT DISTINCT ´text´ and populate the $usedValues array with them.
Related
How can I record all data in a table together?
i have values in individual rows that i wish to add up to one. and get the value into a variable.
what can i use instead of this code below to get all id in the table instead of one?
if(isset($_GET['ide'])){
$coode = $_GET['ide'];
so that once i get all id, i can do the query below...
$products = $db->query("SELECT e1,e2 FROM eyfstb WHERE specialnum='$coode'");
while($row = $products->fetch_assoc()){
$e1view = $row["e1"]; $e2view = $row["e2"];
}
and once the query is done, i want to be able to store them in a variable like below
$final = (e1,e2 of id1) + (e1,e2 of id2) + (e1,e2 of id3) + (e1,e2 of id4);
fine is 5
good is 4
fair is 3
my e1 is fine which is equal 5
my e2 is good which is equal 4
making 9 when i added it.
but i want to get for all record rows in the table
currently i'm able to get the details for only one student from the url $coode but i want to get for all the student from a table and be able to add the resulting data.
Table Structure
id | e1 | e2 |
---------------------------
1 | fine | good |
2 | good | good |
3 | fair | fine |
Better way is to store in database parameters like this as integer. It generates less problems with math operations or comparison.
But if you can't refactor it you should map each value to their numerical equivalent.
function mapDbValue(string $value): int {
switch ($value) {
case 'fine': return 5;
case 'good': return 4;
case 'fair': return 3;
}
}
And now in your while loop map values and add it.
$final = 0;
while (...) {
$final += mapDbValue($row['e1']) + mapDbValue($row['e2']);
}
I have two rows of data - always just two rows, but there could be a maximum of around forty columns. The column names are different on a case by case basis, but here is a representative example:
id | height | width | colour | in_stock | featured | on_sale
------------------------------------------------------------
1 | 30 | 20 | black | yes | no | yes
2 | 30 | 25 | red | yes | yes | no
I want to get all of the differences between those two rows into an array so that I can log what was changed from row 1 to row 2.
I thought it array_diff() would do the job!
So I cheerfully chucked array_diff() at it thus:
//Simplified queries for the example
$sql1 = "SELECT * FROM table WHERE id = 1";
$rs1 = $conn->Execute($sql1);
$rs1 = $rs1->fields;
$sql2 = "SELECT * FROM table WHERE id = 2";
$rs2 = $conn->Execute($sql2);
$rs2 = $rs2->fields;
//Build first array
foreach($rs1 as $key => $value){
$data1[$key] = $value;
}
//Build second array
foreach($rs2 as $key => $value){
$data2[$key] = $value;
}
//Find the differences
$theDifferences = array_diff($data1, $data2);
//Loop through the differences logging the changes
foreach($theDifferences as $field => $value){
echo "Change found for ".$field."!";
}
Why that doesn't work.
This "looked like" it was working. Since many columns contain long strings, colour names, dates etc, so when one changed it was duly pushed into the differences array. The problem was (of course) that the multiple "yes" or "no" columns did not behave as I had expected. Thus the result of the code above, for the table example is:
colour, width
It is not "seeing" the featured or on_sale columns as changed because the data1 array AND the data2 array both contain no's and yes's.
I suppose I need to compare on a key by key basis? Something like the opposite of array_diff_key()? But here I am stuck.
I also considered if this could be done solely with the SQL query, which would I suppose be more efficient, but that is way beyond my SQL ability.
Thanks in advance.
I think you're very nearly there. Maybe something like this after your queries:
$theDifferences = array();
foreach($rs1 as $key => $value){
if ($rs2[$key] != $value){
$theDifferences[$key] = $value;
}
}
As for SQL, you can use an EXCEPT to get a list of rows which are different between two queries, but you'd still have to loop through the keys and look for nulls - which doesn't save you a whole lot.
Hi I pulled a table from an external website using YQL. The table contains a list of times such as: But the number of columns can change to include more columns.
| Name | T1 | T2 | T3 |
|--------|--------|--------|--------|
| Name 1 | 23.234 | 45.234 | 16.456 |
| Name 2 | 23.389 | 44.322 | 15.222 |
| Name 5 | 22.890 | 44.221 | 15.345 |
What I'm trying to do is get the lowest value from each column. I've been able to get it by using this function.
<?php
$sectim = [];
for ($st=1; $st < count($allRows); $st++) {
$sectim[] = $phpObj->query->results->tbody->tr[1]->td[6]->content;
}
echo "<pre>"; print_r($sectim); echo "</pre>";
echo "<pre>"; print_r(min($sectim)); echo "</pre>";
?>
This gives me a list of all the times in that column and gives me the minimum. What I am struggling with is how to do it in a way that I can get the minimum from each table columns given the number of columns is not fixed and can either be 3 columns 4 or even 9 columns. I'm thinking of using either another for statements to loop through all the columns but I don't know whether that will work.
You can use this query
select min(T1) as column1,min(T2) as column2,min(T3) as column3 from tabel
This will give you minimum value of each column.
If you are looking For build dynamic query :
$array = array(
'column1' => 'T1',
'column2' => 'T2',
'column3' => 'T3'
);
$column_binder = array();
foreach ($array as $key=>$value){
$column_binder[] = " min($value) as $key ";
}
$query = "SELECT ".impode(",", $column_binder)." From tabel_name";
I need your help with display of some comma separated enteries from my database.
My main table schema looks like this
|---|----------|-----------|----------------------------|
|id | tab1 | tab2 | tab3 |
|---|----------|-----------|----------------------------|
|1 | state1 | A-North | constA1,constA2,constA3 |
|2 | state2 | B-South | constB1,constB2,constB3 |
---------------------------------------------------------
Query I'm trying to make work
$query = mysql_query("SELECT * FROM `main` WHERE `tab1` = '$tab1'")
or die(mysql_error());
while($row=mysql_fetch_array($query)){
$tab3 = explode(",", $row['tab3']);
echo $tab3."<br>";
}
What I want to display from the database
A-North B-South
---------------------
constA1 constB1
constA2 constB2
constA3 constB3
Error I'm getting when I run that query is "Array" . When I run the same code from phpMyAdmin, I get the desired rows (result).
Explode gives you the results in an array so you'll want another loop that runs through the $tab3 array printing the result. See the definition from the PHP manual:
Returns an array of strings, each of which is a substring of string
formed by splitting it on boundaries formed by the string delimiter.
For example:
for ($i = 0; $i < sizeof($tab3); $i++) {
echo $tab3[$i].'<br />';
}
explode will return array using print_r($tab3) you can view the items and access it by
echo $tab[0];
echo $tab[1];
echo $tab[2];
etc.....
explode ($separator,$string) returns an array. You need to step through the array to create the table columns.
Also, as you want the data in two columns, create these separately as two separate tables and then include those into a single table as two separate cells
Finally, redesign your database table. You'll thank us in the end
How can I make this array with data from a database?
$array=array("a"=>"Apple","b"=>"Ball","c"=>"Cat");
I have a database table with column letter and value.
letter | value
|
a | Apple
b | Ball
c | Cat
I want "a"=>"Apple","b"=>"Ball","c"=>"Cat" to be values from the database, using for loop, how is that possible?
Many thanks for any help!
assuming you can do the connection and select
$array=array();
while ($row = mysql_fetch_assoc($result)) {
$array[$row['letter']]=$row['value'];
}
print_r($array);