Storing text cleanly into mysql TEXT field - php

Im attempting to store the following word in a TEXT field in my MYSQL db table:
Valentine’s
But for some reason it stores as the following:
Valentineâs
Is there a PHP function I can use to clean the string before i store it in the database..
the CHARSET of the table is latin1

htmlspecialchars(); is what you're looking for I guess.

Related

Laravel trying to save HTML in database

I have a Laravel app which manages articles. These articles have a content section, which is being stored within the database as string (varchar). It worked properly when I did not put in some big content, but now it says:
String data, right truncated: 1406 Data too long for column 'content' at row 1
How am I supposed to store the content in the database as HTML?
Laravel supports the text, mediumText and longText column types which resolve to the respective MySQL equivalent.
An appropriate migration entry for your instance would be:
$table->text('content');
You need to expand the size of the content column in the database. This means change the type from varchar to text.

How to retrive the data if it is special charecter (Õ)..?

In my database I have DONT word..
In database it is stored like this:DÕNT Due to that Õ the data is not coming..!
How can i retrive that..?
This my code of retrive:
htmlspecialchars($res_get_option['answer']);
Collation: Null and Type: MyISAM
You will need to set right collation for database and update wrong characters, with right ones, using a script for this.

Unable to retrieve unicode field from mysql to php

I have a database table where i want to store and retrieve in English and Nepali Language. I have a following configuration:
Type: InnoDB
Database collation : utf8_general_ci
Column name: Title_ne
Data Type: text
Column Collation: utf8_unicode_ci
Now when i insert data into Title_ne field using phpmyadmin and provide something like नमस्कार using Google Nepali input for Windows, it is inserted and displayed correctly while querying in phpmyadmin. But, while i query this from php and try to display it in php page, only ???????? is displayed. Am i doing something wrong on table design or inserting data? What is wrong, please provide your suggestions.
नमस्कार is a way of Greeting in Nepal.

MySQL database normalization (taken from Excel)

I have imported a SQL database from an Excel sheet, so it's a little bit messy. For example there's a product field with VARCHAR values such as product8. I would like to grep through these data using some regex, capture the id in this example, and alter column data types. As of now I would start preg_matching the long and hard PHP way, and I'm curious how a database normalization is done right using SQL commands. Thanks for your support in advance.
you can select case, to pull the ids
select right(product,length(product)-7) as productID from table
this will pull the numbers, then you can do whatever

Issue with charset and data

I try to explain the whole problem with my poor english:
I use to save data from my application (encoded on utf8) to database using the default connection of PHP (latin1) to the tables of my DB with latin1 as charset.
That wasn't a big problem : for example the string Magnüs was stored as Magnüs, and when I recovered the data I saw correctly the string Magnüs (because the default connection, latin1).
Now, I change the connection, using the correct charset, with mysql_query("SET NAMES 'utf8'", $mydb), and I've also changed the charset of my tables's fields, so the value now is correctly store as Magnüs on DB; Then I still seeing Magnüs when I retrieve the data and I print on my Web Application.
Of course, unfortunatly, some old values now are badly printed (Magnüs is printed as Magnüs).
What I'd like to do is "to convert" these old values with the real encoding.
ALTER TABLE <table_name> CONVERT TO CHARACTER SET utf8; will convert only the field type, not the data.
So, a solution (discovered on internet) should be this:
ALTER TABLE table CHANGE field field BLOB;
ALTER TABLE table CHANGE field field VARCHAR(255) CHARACTER SET utf8;
But these old string won't change on database, so neither in the Web Application when I print them.
Why? And what can I do?
Make sure that your forms are sending UTF-8 encoded text, and that the text in your table is also UTF-8 encoded.
According to the MySQL reference, the last two ALTER you mentioned do not change the column contents encoding, its more like a "reinterpretation" of the contents.
Warning
The CONVERT TO operation converts column values between the character sets. This is not what you want if you have a column in one character set (like latin1) but the stored values actually use some other, incompatible character set (like utf8). In this case, you have to do the following for each such column:
ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
The reason this works is that there is no conversion when you convert to or from BLOB columns.

Categories