How to save a file to MySQL BLOB using PHP? - php

I'm trying to make a chat history system. So every time a person says hi to one another, they can also say something else. And each of those hi's, i wan't to add what they wrote into a history html file. Being something like this:
James Said: Hi Richard, i saw that hardware you told me about, it is compatible with our software!.
At: 23 November 2011 - 23:09 UTC-08.
________________________________________________________________________________________
Richard Said: Nice!! let's start working with it this week, the project has to be finished before the end of the world.
At: 24 November 2011 - 09:23 UTC-08.
________________________________________________________________________________________
The html file i can build with php, but how do i save it to a MySQL BLOB? Without storing it in a directory (directly to the BLOB).

You're approach to this problem isn't really a good one.
If you try to store the data in a particular output format then you're in real trouble if you suddenly find you need the data in a different format.
You're much better off just storing the particulars of the conversation, and then generating the output to display from the stored conversation. That way you can easily present it in all kinds of formats you might need it in.
EDIT TO ADD:
Something else I should have mentioned (but forgot thanks to all the Christmas brandy ;) ), trying to store the conversation data in a single big block of data will negate most of the advantages using a relational database can confer in the first place. You couldn't, for example, easily store the timestamp of each line of the conversation, or search the database for particular items in the conversation. You could find workarounds of course, but given databases are already designed to solve those kinds of problems anyway, you'd just be wasting effort and your solution wouldn't measure up to what the database already provides.

Since it is not really a binary (the B in Blob), but HTML, I suggest you use the MEDIUMTEXT type and deal with it as just a normal text field.

Related

Is there a way to store multiple records rather than using multiple rows in MySQL?

I would like to make full use out of MySQL for the purpose of a (web) application I have developed for a chiropractor.
So far I have been storing in a single row for [every year] for what are called progress notes. The table structure looks something like this (progress_note_id, patient_id, date (Y-0-0), progress_note). When the client wishes to append for the year of the current progress notes, he simply clicks at the top of a textarea (html), which I use TinyMCE JavaScript library, to make a new entry date along with the shorthand notes to go at the beginning of the column (progress_note). So far its been working ok, if there are 900+ clients (est.) there could potentially be 1300+ progress notes, for each year since the beginning of the application (2018).
Now the client wishes to be able to see previous progress notes (history), but is unable to modify any previous notes, while still be able to write new ones. The solution I have come up with is to use XML inside the textarea, and use PHP to decipher the new notes from the old ones.
My problem however is if I should have to convert my entire table from a yearly to a daily, that it could take up a lot of time and energy to convert multiple notes into each single rows, (est. 10x) Which could end up being 13,000+ rows. I realize that no matter what method I choose to do is going to be a lot of work. Another way around this perhaps I found was to use XML column type in MySQL to potentially store multiple records, and if I wish to append it, all I would need is PHP to interpret the entire XML and add a new child node, to the beginning. Each progress note is 255 - 500 chars. And in worst case scenario, if the patient was to be 52 times a year (1 for every week), there shouldn't be a large enough overhead.
Is this the correct way to solving this problem? I do wish to keep with MySQL DB and I realize that MySQL is not an intended for XML. And for some clarification, what I hope to accomplish is the same thing I intended to do with current progress notes, but with XML. I believe in ascending order (newer -> oldest).
<xml_result>
<progress_note>
<date>2020-08-16</date>
<content></content>
</progress_note>
<xml_result>
Thank-you for any of your time and for any suggestions.
Firstly, 13000+ is not a problem for mysql. In most case for web application, mysql can handle more than 10m+ records for a single instance with a good performance.
Secondly, you can use either XML or JSON format in a text field and handle the decoding in your application.

Laravel - Save HTML Dom-Parser response to database

I have managed to get some HTML from a website using HTMLDomParser (http://simplehtmldom.sourceforge.net/):
$html = HtmlDomParser::file_get_html('https://example.com/id=1');
This results in the following HTML:
<p> ANDE 2, the Atmospheric Neutral Density Experiment 2, is a pair of microsatellites (Castor and Pollux) launched from Cape Canaveral on STS 127 on 15 July 2009 at 22:03 UT and deployed from the payload bay of the shuttle on 30 July 2009 at 17:22 UT.</p>
<p><strong>Launch Date:</strong> 2009-07-15<br/><strong>Launch Vehicle:</strong> Shuttle<br/><strong>Launch Site:</strong> Cape Canaveral, United States<br/></p>
What I want to attempt to do is to take this HTML and save it to a mySQL database. So, in the database I will have four columns:
Description
Launch Date
Launch Vehicle
Launch Site
The HTML layout will always stay the same. My other database contains the id's for the URL's. I am able to replace the id=1 with a variable, so I would be able to manage to save multiple webpage data.
The only thing I am worrying about if the id does not exist. Would it be possible just to skip that page if it cannot find the HTML it is looking for?
I have never done this before, so I am a bit overwhelmed by this. Would it even be possible to do this?
Basically, I just want to save data from a DomParser into my database.
EDIT: Since there has not been an answer, I think we need to tackle this systematically - first we should try creating the function that saves the data to the database.
EDIT 2: I was just thinking - maybe the function that needs to save the HTML can split the result from HTMLDomParser by tag ('strong', 'p', etc.) and create a variable for each column and then save it. I guess the question here first is how do you split the HTML?
EDIT 3: Since I did not receive any answers / suggestions, I am guessing it is very complex to implement this. I still have not managed to come up with anything that saves the HTML successfully to my database.
To check if the ID is valid to the site, I suggest a curl function, so if $url exists, try and get the data (try and catch), if there is an error with the data, catch it and return appropriate response or log it and move to next valid url.
You will have to format the data with a separate function inside the try.
Hope that helps as methodology goes, good luck. Peace

Using Mysql to store articles as Wordpress does

I am looking for a way to store article in database in the same format as it would appear on the website. Could anyone here kindly explain how to store the entire article in database and later display the same on web? I suppose I need some mechanism to accomplish this. But I couldn't find any solution for the past 4 days despite extensive searching all over. Please help.
Storing html in a database is easy enough, but trying to store whole webpages is not the right way to do it...
Many WYSIWYG editors send html through for you to store, depending on your requirements typically you would store these in a VARCHAR to TEXT column in your (MySQL) database.
Simply insert the data into your database with a normal INSERT statement.

How to parse CSV file in PHP and store fields in a database?

I need help parsing the following a CSV file in PHP, so I can insert the contents into a database.
I know I use file_get_contents() but after that I feel a bit lost.
What I'd like to store.
Collection1 - events.text & date
Collection2 - position & name.text & total
I'm not sure how best structure the data to insert into a database table.
"**collection1**"
"events.href","**events.text**","**date**","index","url"
"tur.com/events/classic.html","John Deere Classic","Thursday Jul 9
- Sunday Jul 12, 2015","1","tur.com/r.html"
"collection2"
"**position**","name.href","**name.text**","**total**","index","url"
"--","javascript:void(0);","Scott","--","2","tur.com/r.html"
"--","javascript:void(0);","Billy","--","3","tur.com/r.html"
"--","javascript:void(0);","Jon","--","4","tur.com/r.html"
"--","javascript:void(0);","Bill","--","5","tur.com/r.html"
"--","javascript:void(0);","Tim","--","6","tur.com/r.html"
"--","javascript:void(0);","Carlos","--","7","tur.com/r.html"
"--","javascript:void(0);","Robert","--","8","tur.com/r.html"
"--","javascript:void(0);","Rod","--","9","tur.com/r.html"
As per your previous question, I think this needs to be broken down into sections. As it stands it is rather too broad to answer.
Read the information using file_get_contents(). Make sure this works first, by echoing it to the console. (It sounded from your other question that you felt this would not work if the URL does not have a .csv suffix. It should work regardless of the file extension - try it. If it fails it may be dependent on cookies or JavaScript or some other problem).
Design and create your table structure in MySQL. It seems like you have two tables. They should both have a primary key. Are they related in some fashion? If so, perhaps one has a foreign key to the other one?
Explode your text file on the new line character and loop across the resulting array of lines.
If your CSV data has a title row in the first row position, delete that from your array.
For each line, read the elements of interest using PHP's build-in CSV parsing functions, and store them in variables.
Pass these variables to a custom function that saves the data.
For each save, you'll need to do an INSERT. I recommend using PDO here. Make sure you bind your parameters.
Where you get stuck on a specific problem, you can ask a new and focussed question. At present, the task is to break things down into discrete and researchable pieces.
One trick worth remembering is this shortcut to the PHP manual. If you do not know how fgetcsv works, for example, type php.net/fgetcsv into your browser address bar, and the PHP site will find the function for you. The documentation is excellent.

php mysql - spelling conversion?

I need to change UK spelling to US spelling for the content stored in the database(mysql). Is there any way to do this on the db itself? I found various software's(like a word add-ins) but to use these I need to have the data in a doc format, do the changes and export it back to mysql. Please let me know if there is a method using php/mysql to change the spellings. Checked out things like pspell and other classes(found through google search) but they do not these conversion of uk to us.
regards
I'm fairly sure there's no possibility to do this in mySQL - it is a task that definitely should not be performed by the DBMS.
If no PHP based solution comes up, there are lists like the one from the spelling society that may allow for automatic conversion to some degree - they even come with variations like
cudgel +led, +ling > cudgel +ed, +ing
it is far from complete, though.
May I ask what you need this for?

Categories