how to insert hindi text in mysql database using php? – TOP 3 BEST TIPS

how to insert hindi text in mysql database using php : In this article I will learn to store in to MySQL DB Hindi, Gujarati, Nepali, Telugu, Punjabi, Tamil, Marathi and Bengali etc. without losing its actual format or changing any content.

Easiest method : how to insert hindi text in mysql database using php?

In the first step to learn how to insert hindi text in mysql database using php?, I will make a database with the name of “Article Descriptions” and also make a database table with the name “Article”. simple answer for use to utf8 character set and utf8_general_ci collation.

The Database table will contain the 2 main columns id and name.

In the second step, I will insert the name in Hindi Languages format and then I will get the data from the mysql database and then we are showing it in the HTML nice table.

mysql_query(“SET CHARACTER SET utf8”) has changed/updated to mysql_set_charset(‘utf8’);

This is the preferred methods to update the charset. in this below example also learn to how to fetch and update data from database in php?

Read Also:  select Query In Codeigniter Example Tutorial

Create database:

CREATE DATABASE article_descriptions

          CHARACTER SET utf8

         COLLATE utf8_general_ci;

USE article_descriptions;

CREATE TABLE articles (

   'name' varchar(255) COLLATE utf8_general_ci NOT NULL

)

run the following statement at first

mysql_set_charset('utf8');

Index.php:

<!DOCTYPE html>

<html>
<head>

               <title>how to insert hindi text in mysql database using php ?</title>
               <meta charset="UTF-8">

</head>
<body>
<?php
if(isset($_POST['submit']))
{

               $link= mysqli_connect("localhost","root","article_demo_v1","article_descriptions");
               mysqli_set_charset($link,'utf8');
               $query= "INSERT INTO articles(`name`)VALUES ('".$_POST['name']."');";
               $result= mysqli_query($link,$query);
}

?>

<form method="post" action="">
	<input type="text" name="name">
	<input type="submit" name="submit">
</form>
</body>
</html>

Fetch.php:

In this Article getting this file, I will fetch the data from the mysql database and showing it in the HTML file.

<!DOCTYPE html>
<html>
<head>
               <meta charset="UTF-8">`
</head>
<body>

<h2>Articles Information</h2>
<table>
<tr><td>Article NAME</td></tr>

	<?php

	$link= mysqli_connect("localhost","root","article_demo_v1","article_descriptions");

	               mysqli_set_charset($link,'utf8');

	               $query="SELECT * FORM articles;";

	               $result= mysqli_query($link,$query);

	 while($row= mysqli_fetch_array($result))
	{
	?>

	?>

	<tr><td><?php echo $row['name']; ?></td></tr>
	<?php
	}
	?>

</table>

</body>

</html>

hindi text in mysql database using php
hindi text in mysql database using php

For the developers struggling in storing and retrieving Unicode charsets of the languages, including languages like Hindi, Gujarati, Nepali, Tamil, Telugu, Punjabi, Marathi and Bengali etc.

how to store hindi text in mysql?

how to insert hindi text in mysql database
how to insert hindi text in mysql database

//To set the char set
mysql_set_charset('utf8');

//To select hindi data
mysql_query("SELECT * FROM your_table_name");

// To insert hindi data
mysql_query("INSERT INTO ....");

Aslo Imp Note: Before you showing the hindi Article content in your any types of the browser set content data type of that page using meta tag in header part like below.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

1. Configure MySQL to support UTF-8

ALTER DATABASE YOUR_DATABASE_NAME
 CHARACTER SET = utf8mb4
 COLLATE = utf8mb4_unicode_ci;

Altering only table instead of entire database to support UTF-8:

Read Also:  Laravel 6 Sweet Alert Tutorial With Example

ALTER TABLE YOUR_TABLE_NAME
 CONVERT TO CHARACTER SET utf8mb4
 COLLATE utf8mb4_unicode_ci;

Altering only specific column instead of entire table to support UTF-8:

ALTER TABLE
    YOUR_TABLE_NAME
    CHANGE COLUMN_NAME COLUMN_NAME
    VARCHAR(191)
    CHARACTER SET utf8mb4
    COLLATE utf8mb4_unicode_ci;

If you have online or local store phpMyAdmin installed, and the you can create these some changes using GUI based option by easy to use run this simple go to

phpMyAdmin –> Select Database –> Select Table –> Select operations.

2. PHP Script to Insert UTF-8 Unicode Charset to MySQL

Sample code: how to insert hindi text in mysql database using php?

<?php
$conn = new mysqli($servername, $username, $password, $dbname);
mysqli_set_charset($conn ,'utf8'); //Very IMPORTANT
if ($conn->connect_error) {
    die("Connection error: " . $conn->connect_error);
} 
$sql = "INSERT INTO TABLE_NAME(COLUMN_NAME)
VALUES ('हिंदी')";
if ($conn->query($sql) === TRUE) {
    echo "Good Luck Data has been inserted.";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>

Also how to insert hindi text in mysql database using php? Doc : mysql_set_charset