Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

Make a query passing words with accent

Hello guys

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'

Open in new window


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;
}

Open in new window


What should I do?
thanks
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@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 :)
Avatar of hidrau

ASKER

I also tried this query that works fine in my PhpAdmim

 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

Open in new window


but it didn't work from my site :(
Did you set the charset on the connection like I advised ??
Avatar of hidrau

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.
Avatar of hidrau

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;
}

Open in new window

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.
Avatar of hidrau

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
Avatar of hidrau

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&ecirc;ncio suas inten&ccedil;&otilde;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!
Avatar of hidrau

ASKER

Chris, with your comments I figured out the problem :)
it is solved. Take a look at it :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

thanks a lot