Save data in database in original form and get result in json

I have an html form that helps the user to enter data

<form action="insert.php" method="post" enctype="multipart/form-data">
Service Name
<input type="text" class="form-control" name="a"/>

Service Description
<input type="text" class="form-control" name="b"/>
</form>

      

Page

insert.php helps when inserting data into the database.

<?php
$a= mysqli_real_escape_string($con, $_POST['a']);
$b= mysqli_real_escape_string($con, $_POST['b']);

$sql= "INSERT INTO abc(a, b) VALUES ('$a','$b')";
if(mysqli_query($con,$sql))
    {
        echo "success";
    }
else 
    {
        echo "Error: " . $sql . "<br>" . mysqli_error($con);
    } 
?>

      

Although it works fine, but when I i / p data, something like this

ABC – DEF (ABC – ZYZ)

      

It is saved in the database in the following form

ABC Ò€" DEF(ABC Ò€ " ZYZ)

      

Can anyone please tell me how to save data in original form without spl characters and another thing I would like to do is get data from database in json format for this I used

$sql="SELECT * from abc";
$result = mysqli_query($con, $sql);

if(mysqli_num_rows($result)>0)
{
    while($row = mysqli_fetch_assoc($result))
    {
        header('Content-type: application/json; charset=utf-8');
        echo json_encode(array('list'=>$row));
    }
} 

      

but i am getting "null"

+3


source to share


3 answers


Add the following code before if(mysqli_query($con,$sql))

$con->set_charset("utf8");

      

If above doesn't work, add the following code before if(mysqli_query($con,$sql))

mysqli_query($con, "SET NAMES 'utf8'"); 
mysqli_query($con, 'SET CHARACTER SET utf8');

      

Update as comment.

You also need to change



header('Content-type: application/json');

      

in

header('Content-type: application/json; charset=utf-8');

      

Answer to the second question

<?php
    $sql="SELECT * from activity";
    mysqli_query($con, "SET NAMES 'utf8'");
    mysqli_query($con, 'SET CHARACTER SET utf8');

    $result = mysqli_query($con, $sql);

    if(mysqli_num_rows($result)>0)
    {
        while($row = mysqli_fetch_assoc($result))
        {
            header('Content-type: application/json; charset=utf-8');
            echo json_encode(array('list'=>$row));
        }
    }
?>

      

0


source


You have one 0xc3a2e282ace2809c

that looks like an em dash that is not utf8 encoded correctly. Em emash acctally 0xe28094

, which if you want to do something like utf8_encode(hex2bin('e28094'))

in PHP you will get similar results. This means that your PHP does not match the character set encoding correctly when connecting to MySQL, or your schema is using a different collation to interpret the data and is actually encoded in a different encoding.



Check your schema mapping settings first to confirm this, and then be sure to set the required charset when connecting to MySQL via mysqli :: set_charset for example mysqli_set_charset($con, 'utf8')

. This is the preferred way to do it (try not to use SET NAMES

it as it has caveats that might come back to be shot in the leg later). You must execute before performing any operations in the DBMS for it to be efficient (i.e. before you call mysqli_real_escape_string

in the above example). Also see symbols sets concepts for more details.

0


source


Use this.

header('Content-Type: text/html; charset=utf-8');

      

in tage form use this

<form accept-charset="utf-8">

      

0


source







All Articles