MySql: Quickest way to append to current value in DB - php

I have a table with 32,000 entries.
Each entry has a JSON blob against it:
{
"property1": "someValue",
"property2": "AnotherValue"
}
I now need to append another property to this blob for every entrant so that we have:
{
"property1": "someValue",
"property2": "AnotherValue",
"property3": "YetAnotherValue"
}
Because someValue and AnotherValue will be different for different entries I cannot just replace the old records with the new one. Instead (I'm guessing) I'm supposed to parse JSON, add property3 and return it to DB for each of the 32,000 records.
What is the best way to achieve this? I have a MySql database which I interact with through PhpMyAdmin.
I tried writing a PHP script which will basically select and loop through every row, parse and append to my JSON blob and put it back up for every record. However after I run this in a browser it simply times out on me because it takes too long to execute (I suspect it's the wait time between pulling the blob and uploading it back up for every record).
Any help is greatly appreciated!

I would question the need to save JSON into a MySQL DB directly. Let's say your table with the JSON blob is called my_table and it has an id field. I'd create another table, say, my_json_properties. There, I'd create three columns: my_table_id, property_name and property_value. Then just populate that table with the JSON properties, and adding new properties is trivial. Also remember to delete the blob column and of course to update your data access objects.
Note: use the InnoDB storage engine so you get to make the my_json_properties.my_table_id into a foreign key that references my_table.id. MySQL also automatically creates an index with the foreign key columns, so it's very effective to query the properties when you just know the id in the original table.

You could use an UPDATE statement to add this new property to your json blob, if the json is regular, i.e. there's no blank after the last property and the } has got a fixed position in the next line. Building on your example and assuming \n as end of line you could do it like that:
UPDATE example
SET json = REPLACE(
json,
'"\n}',
'",\n "property3": "some value"\n}');
Demo(http://sqlfiddle.com/#!2/db972/1)
A more robust approach would be by the suggestions of Marcus Adams:
UPDATE example
SET json =
CONCAT(
TRIM(
TRAILING FROM TRIM(
TRAILING '\n' FROM TRIM(
TRAILING FROM SUBSTRING_INDEX(json, '}', 1)
)
)
),
',\n "property3": "some value"\n}'
);
This will work independently of trailing spaces after the last property and leading spaces before the }, but it's still relying on the \n as end of line.
Note:
Such statements are usually much faster than row by row updating with a script.

It depends on use case.
If this is a one time task, it would be MUCH better to run your script from the shell and not from browser to avoid web server and browser timeouts.
php -f my_update_script.php
If you cannot run script from the shell for whatever reason you can try to fool the browser and web server as follows:
set_time_limit(3600*2); // two hours should be enougth?
// turn off output buffering
while (ob_end_flush());
ob_implicit_flush();
// Your mysql fetch..update loop here
// begin loop
echo 'Something useful every iteration to let them know your script is alive :)';
// end loop
However this approach may fail if there is caching reverse proxy in front of your web server.
MariaDb has native support for JSON format (since version 10), you may consider to switch to MariaDb.

Related

Store php generated JSON string in a CSV file

I am trying to generate a JSON string using php from an array, and I would like to store that string in a csv file, also with PHP. I want to do this, because I am working with a quite large amount of data, and I would like to use MySQL's LOAD DATA LOCAL INFILE to populate and update my database table.
This is the code I have:
$tmpFileProducts = 'path/to/file';
$tmpFileProductsHandler= fopen($tmpFileProducts, 'w');
foreach ($attributeBatch as $productId => $batch) {
fputcsv($tmpFileProductsHandler, array($productId, $batch['title'], $batch['parsed'], json_encode($batch['attributes'])), "|", "\"");
}
My problem is, that when I am creating the CSV file, the JSON double-quotes are not escaped, thus I end up with simmilar lines in my csv file:
43541|"telefon mobil 3l 2020 4g "|"2020-12-05 17:38:19"|"{""color"":""dark chrome"",""memory_value"":4294967296,""storage_value"":68719476736,""sim_slot"":""dual""}"
My first possible solution would be, to change the string enclosure of my CSV file, but what enclosure should I use, to ensure no conflicts could arrise with the inner json column? I am in complete control of the array that is stringified, it will only contain ASCII characters, in it.
Would there be a way, to keep the current string enclosure, and instead escape the JSON string somehow? Later on, I will need to fetch the data that is included to the database, and convert it to an array again.
DISCLAIMER: I am well aware that instead of storing the data as a JSON string, I could store it in a specific relational table (which I am also doing), but I would need quick access to this data, for a background script that is running, and I would like to save on the time of the queries, to the relational table, as when the background script will use this data, it doesn't need to search in it.
Follow up question: as I am explicitly telling the fputcsv function what to use as string enclosure shouldn't it automatically escape all the simmilar inner strings?

Order of execution between text and a class-generated table

I have a problem with a HTML table generated with a PHP class and saved into a variable.
This variable also contains text, saved before the table, but the table is displayed before the text.
Here's what I do :
Put a text in a variable :
$RET='Awesome introduction';
Generate a table with a class of my own (it's still a simple table) :
$TAB=new class_table();
$TAB::add_th('Column'); // saves 'Column' in a 'th' 3D array
$TAB::close_line(); // $num_tr++
$TAB::add_td('Cell'); // saves 'Cell' in a 'td' 3D array
$TAB::display(); // generate the table
When I do this :
$RET='Awesome introduction';
$RET.=$TAB::display();
echo $RET;
... I've got the table before the text.
When I do this :
echo 'Awesome introduction';
$TAB::display();
... I've got the table after the text, and that's correct.
When I generate my table without the class, it's correct too, of course.
Do you know if it's normal (maybe the class' call is executed in first ?) and if I can workaround this ?
I ask because this code is a just a easy sample, the real one is much complex and called with AJAX (and a console.log proves that the table is generated before the text). I hope I'm clear enough.
Thank you.
Yes, probably Completely normal. This Display class is probably doing exactly that. Displaying, not returning values. You are assigning nothing to the $RET.= , You can prove it by echoing it a second time, it will print your header a second time but not your table. At the time that you assign nothing to RET you output the table, probably with print statements or echo statements in the Display class. There maybe be some way to use the eval function to capture the output of the Display class, but that's above my pay grade. (This would just be a comment, but i haven't earned the privilege.)

PHP/MySQL: Strange output issue when echoing variable from MySQL database

I have a MySQL table that contains names and e-mail addresses. The data was originally imported from a .csv file, and it did not originally contain complete e-mail addresses. We had to append the #place.domain to the user's alias.
When the data is sitting in the MySQL table, it looks normal: person#place.domain; however, when I output the content in PHP, I get this: person #place.domain. There's always a space between the person and the #. It doesn't look like that in the MySQL column, nor does it look like that when I copy/paste the data into Notepad, Word, Excel, etc. Furthermore, if I erase the data in the column and manually replace it with person#place.domain, it displays normally in my PHP app. So I'm guessing there's some hidden character that PHP is picking up that I can't detect. Is there a way for me to clean this up? I've tried TRIM(), REPLACE,(), etc., all to no avail.
UPDATE: I've discovered that, when I click in the MySQL field and move among the characters using my arrow keys, I have to hit the arrow key TWICE to move past the # symbol, yet there is no visible space.
I made this sample code for you:
<?php
$test = "user #mail.com";
$aux = explode("#",$test);
$mailok = trim($aux[0])."#".trim($aux[1]);
echo $test." vs ".$mailok;
?>
This is likely something like non-breaking space (ascii 160). To get rid of it:
UPDATE my_table SET e_mail = REPLACE(e_mail, CHAR(160), '');
Try with a foreach cycle, and do the chr($char) function to every character in the string, it will display you the ascii code of each character in the string, and you will find the wrong character. It's the only solution I found. Hope to be useful

php how to write data into json?

I am newer for php. I want make php page cache, query data from mysql and store data into json format.
I have many questions:
which type of file should I store? .json or .txt or .cache? for I also need use json decode return datas into page.
I want use cron tab, make many mysql queries and write into one json file. what write code should I choose? fopen, fwrite or file_get_contents or other command? (do not cover the data, but continue write. I will deleted the file and renewer it at the next cron time)
If a multi write into a json data (10 or more mysql query at the same time and write into a same json file, each json child format like {name: ".$row['name']."}), how to completed a top { and bottom } to make a standad json data format?
{ //how to add this one
{name: ".$row['name']."}
{name: ".$row['name']."}
// many name from 10 more mysql queries
} //and this one
Thanks.
It's json_encode()
json_encode() — Returns the JSON representation of a value
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
which type of file should I store
It doesn't matter. There is no fixed extension, but I would pick .json just to make it clear what the file is supposed to contain.
what write code should I choose?
Just use file_put_contents to put the JSON string (see next section) into a file.
each json child format like
You really do not want to use that method. It might work for a while, but becomes very complex when you need to handle things like quoting and special-character escapes. Instead of re-inventing the wheel, use PHP's built-in JSON functions for this.
Create the data-structure you want using PHP's strings, numbers, and arrays, and then rely on json_encode to turn it into a string.
The main thing to be careful of is that depending on how your php array() looks, you might get JSON [] versus {}.
As far as saving the file as .txt or .json won't make a difference.
I think the focal point of this all lies in the json_encode page. Here's the example from that page:
This code:
<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);
?>
Outputs like this:
{"a":1,"b":2,"c":3,"d":4,"e":5}
3 . You can use fopen and fwrite to write to your file. The second argument to fopen is the mode, you want to use 'a' for append.
Don't write your own cache because anything you write in PHP will be slower than can be supported by native extensions (like APC or memcached or even MySQL itself!!).
Don't cache as JSON. JSON is not a particulary 'fast' to serialize. If you're doing caching you don't want to do any serialization at all. Just store it as it is.
MySQL does query caching for you. If performance is a problem first tune your MySQL queries and database schema. Caching is one of the absolute last optimization you want to do.
If you want an easy way to cache, make a MySQL table called 'cache' and use that. If you want quick (small) file access, use MySQL (seriously). If you want an even faster cache access use an in-memory cache like APC or memcached.

Assistance with building an inverted-index

It's part of an information retrieval thing I'm doing for school. The plan is to create a hashmap of words using the the first two letters of the word as a key and any words with the two letters saved as a string value. So,
hashmap["ba"] = "bad barley base"
Once I'm done tokenizing a line I take that hashmap, serialize it, and append it to the text file named after the key.
The idea is that if I take my data and spread it over hundreds of files I'll lessen the time it takes to fulfill a search by lessening the density of each file. The problem I am running into is when I'm making 100+ files in each run it happens to choke on creating a few files for whatever reason and so those entries are empty. Is there any way to make this more efficient? Is it worth continuing this, or should I abandon it?
I'd like to mention I'm using PHP. The two languages I know relatively intimately are PHP and Java. I chose PHP because the front end will be very simple to do and I will be able to add features like autocompletion/suggested search without a problem. I also see no benefit in using Java. Any help is appreciated, thanks.
I would use a single file to get and put the serialized string. I would also use json as the serialization.
Put the data
$string = "bad barley base";
$data = explode(" ",$string);
$hashmap["ba"] = $data;
$jsonContent = json_encode($hashmap);
file_put_contents("a-z.txt",$jsonContent);
Get the data
$jsonContent = file_get_contents("a-z.txt");
$hashmap = json_decode($jsonContent);
foreach($hashmap as $firstTwoCharacters => $value) {
if ($firstTwoCharacters == 'ba') {
$wordCount = count($value);
}
}
You didn't explain the problem you are trying to solve. I'm guessing you are trying to make a full text search engine, but you don't have document ids in your hashmap so I'm not sure how you are using the hashmap to find matching documents.
Assuming you want a full text search engine, I would look into using a trie for the data structure. You should be able to fit everything in it without it growing too large. Nodes that match a word you want to index would contain the ids of the documents containing that word.

Categories