0

Explaination about how this question is different from the exact duplicate specified by Mr. @deceze
=================================
The solution provided in the so called duplicate article uses UTF-8 only and that I am already using in the code specified below.
My question specifically talks about two things:
1. Collation : utf8_general_ci
2. Methodology: OOPS instead of PDO
My code given below already implements UTF-8 and I have mentioned that it is working fine.
My question is about Hindi that is being stored perfectly bu i donot understand how to fetch it with the same collation in OOPS approach.
=================================
using Google API, I have inserted some information in the database in hindi language. The column in mysql has data type as LONGTEXT and Collation as: utf8_general_ci

When I insert the values from frontend in PHP, data is store something like this: पà¥à¤°à¤¶à¥à¤¨ १

At the same time, I am also saving the encrypted value of this hindi data through md5 as: md5($_POST['transliterateTextareaans'])

ON the show page, I have set head information as follows:
<meta charset="UTF-8">

And the the data in hindi is displayed accurately. Now here issue arises.
I want to convert this data again to md5 value and cross check with the database encrypted value, but they are not matching. (Consider the scenario of storing username and passwords in hindi OR MCQs where correct answer is stored in encrypted form and while checking the candidate answer it needs to be compared).
Here is my code:

if(md5($_POST['ans']) == $_POST['hids'])
{
    //correct
}
else
{
    //wrong
}


I have gone through google and found that I should set the collation of select query but i donot know how to do it. Here is my select code:

if($ques=$mysqli->prepare("SELECT * FROM mytab WHERE sno=?"))
{
    $ques->bind_param('i', intval($_SESSION['qlist']));
    if($ques->execute())
    {
        $res=$ques->get_result();
        $row=$res->fetch_assoc();
    }
}



The code has now been updated as below:

if(!$conn->set_charset("utf8")) // SETS THE CHARACTERSET
    echo $conn->error;
if(!$conn)
{
    die("Connection failure: ". $conn->connect_error);
}
if($sql=$conn->prepare("INSERT INTO q_bank VALUES (?, ?, ?, ?, ?, ?, ?)"))
$sql->bind_param('sssssss', $p1, $p2, $p3, $p4, $p5, $p6, $p7);
$p1=........... //upto $p5
$p6=md5($_POST['txtbox']);


Similarly, now the select code to get the data:

if(!$mysqli->set_charset("utf8")) //SETS THE CHARACTERSET FOR FETCHING
    echo $mysqli->error;
$ques->bind_param('i', intval($_SESSION['qlist'][$_SESSION['qnum']]));

Now, the data which doesnot have any digits in it, compares fine.
The data which has any digit in it, fails to compare.

Note: data here refers to the Option in an MCQ and it needs to be compared to the encrypted correct answer.

ITSagar
  • 673
  • 2
  • 10
  • 29
  • Are you setting the **connection encoding** to your database to `utf8` as well…? – deceze Dec 02 '16 at 14:23
  • 1
    Note: md5 generates a one way HASH. It is not encryption. – RiggsFolly Dec 02 '16 at 14:24
  • You set mysqli to use a specific character set using `mysqli::set_charset` [see the manual](http://php.net/manual/en/mysqli.set-charset.php) almost everything is documented in there if you look! Also documented in the Duplicate question. Always a good idea to do a search before asking a question – RiggsFolly Dec 02 '16 at 14:30
  • @deceze First of all this is not a duplicate question at all, please read my edit carefully. Secondaly, I am sending data as string without setting any connection encoding. – ITSagar Dec 02 '16 at 17:21
  • *"data is store something like this: पà¥à¤°à¤¶à¥à¤¨ à¥"* — That's not Hindi, that's garbage. Something *is* wrong with how you handle encodings. Fix that first and foremost to store actual Hindi in your database. – deceze Dec 02 '16 at 18:08
  • @deceze well this is the encoded data. and it cannot be garbage because it is shown perfect in hindi when taken out with select query – ITSagar Dec 02 '16 at 18:10
  • Read this to understand what is happening: http://kunststube.net/frontback. Understand that you *are* mishandling your encodings. Then give us more details so we can tell you in more detail how to fix it. – deceze Dec 02 '16 at 18:13
  • I have updated the Insert and Select code by adding one more line of code: $conn->set_charset("utf8") ... Now the data is shown in Hindi in the database also. But now there is new problem. Content which doesnot have a digit worked perfect for comparison of right answer but the content which contains any digit still fails to be compared sucessfully – ITSagar Dec 02 '16 at 19:06
  • `var_dump` a sample of data (both sides of the comparison) which fails and show us. – deceze Dec 02 '16 at 19:25
  • @deceze - It's nearly Devanagari. Undoing the Mojibake gives `प�रश�न`. But the black diamonds indicates that something else is going wrong. – Rick James Dec 02 '16 at 23:39
  • @ITSagar - Please start a new question and provide `SELECT col, HEX(col) FROM ...` so we can see whether the data was mangled going _into_ the table. After that, we should be able to further figure this out. – Rick James Dec 02 '16 at 23:41
  • `प` is _correctly_ represented in utf8 (or utf8mb4) as hex `E0A4AA`. However, the "double-encoding" of PA is hex `C3A0 C2A4 C2AA`, which is interpreted as `प`. – Rick James Dec 02 '16 at 23:44
  • The problem is completely different. I have debugged the code and here is what I found: if the option of a question is W2U then it works fine because digit is without spaces but if the option is W 2 U then only W is taken by the code, everything else is discarded. Why is that so? – ITSagar Dec 03 '16 at 05:07
  • We can hardly help you without knowing *exactly* what you're doing with those values. Text doesn't typically become mojibake just by itself. – deceze Dec 03 '16 at 07:30
  • I don't understand "option of a question is W2U". – Rick James Dec 04 '16 at 00:55

0 Answers0