How to get the folder names in UTF-8 using PHP
I have a database that is in Spanish language.
When I query the database i use Spanish accent words. these words come from many folder names that I have on my hard drive. e.g i have a folder called 'Canadá' and I query the db saying select from table where country='$name'
.
In my code im doing $name=GetFolderName()
which reads whatever the folder name is.
This does not work. If I hardcode the folder name like $name="Canadá"
, then the query executes fine, but if i read the folder name via 开发者_C百科PHP, the query does not execute successfully. Maybe it is caused because my db table is set in UTF-8.
Is there a way to read the folder name such that the name I read is converted in UTF-8 format?
Be sure you are quoting right. If GetFolderName()
doesn't return "Canadá"
your Query will most likely fail. Always check the actual Query String if you're having Trouble with SQL Queries.
First, correct the quoting in your query:
$query = "select from table where country='$name'";
Next, try printing variable $name
before sending it to the database as part of the SQL - to see what it's actually set to.
I have been searching for an answer to this question, but I stumbled on the answer myself, just last night. Here's what I learned:
Someone elsewhere suggested the use of basename(__DIR__).PHP_EOL;
I could not understand why this didn't work until I echoed the name twice. There was a space ad the end of the filename. the .PHP_EOL
adds the space, which affects your DB query. So my solution is:
$folder = basename(__DIR__);
$sql = "SELECT * FROM tablename WHERE folderid = '".$folder."'";
This worked perfectly for me. I use it to allow for a web page to have links specific to the person, content, or organization within a business directory, using a folder specific to that end.
I realize this question is years old, but I hope this helps someone.
精彩评论