hidrau
asked on
Make a query passing words with accent
Hello guys
I am making a simple query:
My problem is when I pass the value with accent, it doesn't work, but if I pass a word that doesn't have any accent, it works fine.
I executed the query above in my phpadmim and it worked fine, only when I try to do from my page :(
this is my code:
What should I do?
thanks
I am making a simple query:
select A.*, B.CARTAN from CARTAPALCHV A INNER JOIN CARTAS B ON B.CARTAS_ID=A.CARTAS_ID where A.palchv='Prepotência'
My problem is when I pass the value with accent, it doesn't work, but if I pass a word that doesn't have any accent, it works fine.
I executed the query above in my phpadmim and it worked fine, only when I try to do from my page :(
this is my code:
function MontaIdcartasPorPalChv($pal){
global $servername, $username, $password, $dbnome;
$conn = new mysqli($servername, $username, $password, $dbnome);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "";
$sql = $sql . " select A.*, B.CARTAN from CARTAPALCHV A";
$sql = $sql . " INNER JOIN CARTAS B ON B.CARTAS_ID=A.CARTAS_ID";
$sql = $sql . " where A.palchv='" . $pal . "'";
print $sql;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $id . $row["CARTAN"] . ",";
}
}
else {
$id = "-1";
}
$conn->close();
return $id;
}
What should I do?
thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Chris: You might want to use "utf8mb4" for the character set. It will depend on whether you need full support for the Eastern characters. In MySQL, "utf8" gives 3-byte encoding; "utf8mb4" gives full 4-byte encoding.
Thanks Ray :)
ASKER
I also tried this query that works fine in my PhpAdmim
but it didn't work from my site :(
select A.*, B.CARTAN from CARTAPALCHV A INNER JOIN CARTAS B ON B.CARTAS_ID=A.CARTAS_ID where A.palchv = _utf8 'Inconsciência' COLLATE utf8_unicode_ci
but it didn't work from my site :(
Did you set the charset on the connection like I advised ??
ASKER
Yeah, I tried Chris and nothing :(
As I told you, all queries work fine in PhpAdmim, less through my page.
You can play with my page, there is a input where the query is displayed, the end of it, the number
-1 informs that any row was found. The success happens only when the word doesn't have any
accent.
As I told you, all queries work fine in PhpAdmim, less through my page.
You can play with my page, there is a input where the query is displayed, the end of it, the number
-1 informs that any row was found. The success happens only when the word doesn't have any
accent.
ASKER
The code is this way:
function MontaIdcartasPorPalChv($pal){
global $servername, $username, $password, $dbnome;
$conn = new mysqli($servername, $username, $password, $dbnome);
$conn->set_charset("utf8mb4");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//print $pal;
//$str=stringParaBusca($pal);
//print $str;
$sql = "";
$sql = $sql . " select A.*, B.CARTAN from CARTAPALCHV A";
$sql = $sql . " INNER JOIN CARTAS B ON B.CARTAS_ID=A.CARTAS_ID";
$sql = $sql . " where A.palchv = '" . $pal . "'";
//$sql = $sql . " where A.palchv = _utf8 '" . $pal . "' COLLATE utf8_unicode_ci";
print $sql;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $id . $row["CARTAN"] . ",";
}
}
else {
$id = "-1";
}
$conn->close();
return $id;
}
OK. Can't see anything obvious in your code that would cause a problem. It all looks OK. Are you absolutely sure that the query should return a record based on the value of $pal.
Also, what output do you get from the print($pal) line. Post it here for us to take a look at.
I notice in your page your output has been generated using htmlentities. Make sure you're not running that on $pal before trying to query your database.
Also, what output do you get from the print($pal) line. Post it here for us to take a look at.
I notice in your page your output has been generated using htmlentities. Make sure you're not running that on $pal before trying to query your database.
ASKER
Chris, I changed my database to Utf8 this way:
ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;
all the fields are changed too
and nothing yet. That's a mistery LOL
ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;
all the fields are changed too
and nothing yet. That's a mistery LOL
ASKER
You can run the page and in the input you get the result of $pal
And regarding my previous comment .... what is the output of print($pal) ... if this is inside an HTML page, view the source after the page is rendered rather than just checking the browser windows
If what is displayed on the page is the exact value of $pal, then that's your problem:
On your page I see this:
mantenha em silêncio suas intenções
But the actual value is this:
mantenha em silêncio suas intenções
Now I'm assuming that isn't the value that's stored in your database, so a query against it will not return a result - there will be no match!
On your page I see this:
mantenha em silêncio suas intenções
But the actual value is this:
mantenha em silêncio suas intenções
Now I'm assuming that isn't the value that's stored in your database, so a query against it will not return a result - there will be no match!
ASKER
Chris, with your comments I figured out the problem :)
it is solved. Take a look at it :)
it is solved. Take a look at it :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks a lot