I am trying to use special danish characters (æøå) in the URL as GET parameters. So for instance I have this URL:
http://example.com?name=åge
that URL will get converted into the ASCII equivalent automatically, so in the URL it will read:
http://example.com?name=%E5ge
When i access and print out that value it works just fine, and displays that parameter as:
åge
However, i am using that to select stuff in my DB, and this won't work. If i use the ASCII version in the URL, it won't select anything form the DB and just give me an empty result. If i force the URL to not use ASCII, so it is: http://example.com?name=åge it will work fine when selecting from the DB, but when I display the parameter it shows as this:
Ã¥
I have no idea how to get around this. Any help is appreciated.
I would transform the parameter to UTF-8 and then prepare the database for a UTF-8 query, with something like this:
$name = utf8_encode($_GET['name']);
mysqli_query($mysqli_connector, "SET NAMES UTF8");
And then, prepare your query.
That should work.
I hope that helps!
Related
I am using codeigniter 3 for my project. In my database i have a table that is being used by some other authentication system. the problem i am facing when i am am searching some value with special character (inside the data/ concated with the data)
$query = "SELECT * FROM tablename WHERE value = '$#!"."asdasd<3ddasd"."'";
Even $this->db->query($query); is not returning any desirable output. after echo $this->db->last_query(); i get the query and it was as it should be. if i copy it to phpmyadmin, it gives correct result.
As per several discussion in SO and some other pages i have also tried with
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-!##<$';
db is utf8_general_ci is Server connection collation.
Query i put here just for showing you a scenario and i have used active record.
EDIT 1: I have used $this->input->post() for getting the input.
EDIT 2: just found out doesnot work with "$#!"."asdasd<3ddasd" but work for "$#!"."asdasd3ddasd"
Make sure you do not forget to urldecode the post values. What might be happening is that you use $_POST instead of the codeigniter function $this->input->post();
$_POST won't urldecode the parameters for you while the codeigniter function does.
So verify that the variable you pass to your query really is selänne and not sel%C3%A4nne
And if it is sel%C3%A4nne, use urldecode() or $this->input->post()
Reference from This Question
I've been trying to get started with Phalcon, but I've been stuck for a few days trying to get query against my database. If I can't find a resolution to this issue I'm going to have to move on.
The target table uses inno-db and is utf-8 encoded. Table has two columns: one is index values, the other is individual (unique) east-Asian characters. When attempting to retrieve a record using a utf-8 encoded Chinese character Phalcon returns 0 records. In addition, when retrieving records using the index value, the corresponding character value is returned as a question mark (a regular question mark, not the question mark browsers use as a placeholder for unrecognized characters).
$characters = Characters::find("indCharacter = 乱");
Returns this error:
"PhalconException: Scanning error before '��' when parsing: SELECT [Characters].* FROM [Characters] WHERE indCharacter = 乱 (64)"
Using single quotes around the actual character 0 results are returned.
I've run the exact same queries using the command line, phpmyadmin, and workbench, all of which were in the same environment. All properly returned records.
I've also double checked that the original query value is utf-8 encoded, and that all data in the table is utf-8 encoded.
Phalcon: 1.2.6
Php: 5.4.11
When using Phalcon's Pdo Mysql adapter to create a db connection you have to explicitly set the character encoding. Here is an example similar to one found in Phalcon's tutorial, except that I've added "charset".
$di->set('db', function() use ($config) {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "myhost",
"username" => "myusername",
"password" => "mypassword",
"dbname" => "mydbname",
"charset" => "utf8"
));
});
I had previously looked in the documentation for the various adapter classes, but couldn't find anything relating to setting the encoding for the database connection. I (incorrectly) assumed that encoding detection was handled internally.
EDIT: Just saw your note about single quotes. You could try a parameterised query as below.
You need to put quotes around the string you're matching. The syntax accepted by Model::find() with one parameter is similar to what would normally go in a MySQL WHERE clause.
$characters = Characters::find("indCharacter = '乱'");
The docs for finding records of a model show this.
Parameterised query
Note that you can skip adding quotes and escaping if you use a parameterised query:
$characters = Characters::find([
"indCharacter = :char:",
'bind' => [
'char' => '乱'
]
]);
Plain MySQL query
Alternatively, you can run plain MySQL queries using a database adapter. This isn't really suitable if you want to pull model objects from the database, but it does allow you to work around issues with PHQL or skip the ORM when you want to. For example REGEXP causes the PHQL parser to error, but it's perfectly valid in MySQL.
/** #var Phalcon\Db\Adapter\Pdo\Mysql $connection */
$connection = $this->di->get('db');
$result = $connection->query("SELECT * FROM characters WHERE indCharacter = '乱'");
Small notice... I haven't done anything with Falcon, but I did have a similar error when using MySQL/PHP which I did solve. Characters like ã and í didn't work.
What you can try it to use Unicode NCRs. For 乱, it's 乱. I found this website that converts Chinese characters to NCRs.
Again, this might work. My problem was that things were typed into a form and added to a database, which I got around by replacing the instances of characters that didn't work with their NCRs. It might be a hassle to convert everything but I think it's worth a try.
I generate a mysql query from a form with a free text search field.
Something like:
SELECT ... FROM ... WHERE 'something' LIKE '%SEARCH%'
All this works fine and returns the valid rows when the search does not contain any special characters, like the danish characters ÆØÅ.
When these letters ARE used, the query returns no results, all though when i take the generated query string and plug it into phpMyAdmin i get exacly the result i want.
Thanks
add this line of code in your connection file...
mysql_set_charset("utf8", $db);
it is better for you to encode your data to UTF-8 before you pass it into query...
I'm not using Danish but Czech but I think there are the same (at least by UTF8 implementation) - you must keep in mind used encoding (original server script, data tables self and also your database connection handler).
I think you have an encoding problem, maybe phpMyAdmin is using a different client encoding than your other client. SET NAMES 'encoding' should just do what you need, I think.
Also we can use PHP variable and convert it before the select operation (supposing data base is ISOO-8859-2) .
Example:
// word with special characters
$search='kötészeti';
// conversion to ISO
$search=iconv("UTF-8","ISO-8859-2", $search);
// create search condition
$condition="SELECT ... FROM ... WHERE 'something' LIKE '%$search%'";
// apply query
mysql_query($condition);
I have a url that is displayed as this :
http://www.amazon.com/gp/product/0307408418/ref=s9_al_bw_ir01?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-3&pf_rd_r=0ZHX172F5RRJXR7D5EKD&pf_rd_t=101&pf_rd_p=1313842642&pf_rd_i=283155
What happens though is when I try to use the original url string in an sql query, I get an empty set. It is largely do due to the url encoded differently. I wanted to know then, how could I update the url string to properly query the database?
Maybe try using htmlspecialchars():
'SELECT * FROM tablename WHERE url = ' . htmlspecialchars($originalUrl);
htmlspecialchars should replace special characters in your URL with the HTML char code equivalent
For more info:
http://www.php.net/manual/en/function.htmlspecialchars.php
he i have a problem
i have a mysql database dump, but all the data is url encoded has + instead of spaces and other characters as %20 etc
i tried to import this directly and wrote a php script to decode the data and rewrite it back to the db,
/// make rs..........
........
do {
// decode the items in the countries table
$myCountryID = $row_countries['id_con'];
$new_country_con = urldecode($row_countries['country_con']);
$result = mysql_query("UPDATE countries_con SET country_con = '".$new_country_con."'
WHERE id_con = ".$myCountryID."")or die(mysql_error()) ;
echo("id_win: ".$myCountryID."<br>added country_con: ".$new_country_con."<br>");
} while ($row_countries = mysql_fetch_assoc($countries));
echo('----------------------------------END Countries-------------------------------------<br><br>');
the page is set to use utf8 all the tables in the db are set to use utf8
the echo statement outputs the correct characters, but it still gets stored in the db as a urlencoded string, how do i get it into the db as utf8 without the url encoded characters?????
Try to printout your query to see at which point it produces %20 (before query execution or after).
Also please see: http://recursive-design.com/blog/2008/06/23/force-mysql-encoding-to-utf8/