如何使用 PHP 获取 UTF-8 格式的文件夹名称
我有一个西班牙语数据库。
当我查询数据库时,我使用西班牙语口音词。这些词来自我硬盘上的许多文件夹名称。例如,我有一个名为“Canadá”的文件夹,我查询数据库时说select from table wherecountry='$name'
。 在我的代码中,我正在执行 $name=GetFolderName()
,它读取文件夹名称。
这是行不通的。如果我对文件夹名称进行硬编码,如 $name="Canadá"
,则查询执行正常,但如果我通过 PHP 读取文件夹名称,查询将无法成功执行。可能是因为我的数据库表设置为UTF-8造成的。
有没有办法读取文件夹名称,使我读取的名称转换为UTF-8格式?
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保你引用的是正确的。如果
GetFolderName()
不返回"Canadá"
您的查询很可能会失败。如果您在 SQL 查询方面遇到问题,请务必检查实际的查询字符串。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.首先,更正查询中的引用:
接下来,尝试打印变量
$name
,然后将其作为 SQL 的一部分发送到数据库 - 以查看它的实际设置。First, correct the quoting in your query:
Next, try printing variable
$name
before sending it to the database as part of the SQL - to see what it's actually set to.我一直在寻找这个问题的答案,但就在昨晚,我自己偶然发现了答案。这是我学到的:
其他地方有人建议使用
basename(__DIR__).PHP_EOL;
我无法理解为什么这不起作用,直到我重复了该名称两次。文件名末尾有一个空格广告。.PHP_EOL
添加了空格,这会影响您的数据库查询。所以我的解决方案是:这对我来说非常有效。我使用它来允许网页具有特定于业务目录中的人员、内容或组织的链接,并使用特定于该目的的文件夹。
我意识到这个问题已经有很多年了,但我希望这对某人有帮助。
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: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.