Select PHP in tables with special characters

Asked

Viewed 1,052 times

2

For some reason DBA put "ç" in the name of tables in the Mysql database and now I can’t select these tables from php:

$conn = new mysqli($servername, $username, $password, $dbname);
$sql="select * from Tbl_login";
$result = $conn->query($sql);

select the wheel. But the:

$conn = new mysqli($servername, $username, $password, $dbname);
$sql="select * from Tbl_OrdemServiço";
$result = $conn->query($sql);

It does not bring results...but if I run this select in the bank it brings.

Among the various tests I did gave me also this mistake:

You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near ' o' at line 1

someone can help me ?

2 answers

1


The problem seems to be the incompatibility between Charsets base and code/client/file.

When OP displays the error message:

have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near 'the' at line

this shows that the ç was converted wrong.

The solution was to set the connection charset

 $conn->set_charset("utf8")
  • +1 Perhaps you were right, although it may be bad practice, some people do not work with what they create, they work with what they find. And solving a problem is always better than avoiding it (at the moment I have the votes blocked).

  • @Edilson, there’s a bank you use [ ] brackets to escape names with special characters.

  • I usually prefer not to use any kind of special character, like straight and simple names, are easy to memorize, and easy to handle.

0

To search for an exact word and/or that may cause some syntax error in sql use simple quotes around the word, and in your query you are placing dot and comma within the query in;

$sql="select * from Tbl_OrdemServiço;";
                              _Aqui ^

Do the following

$sql = "SELECT * FROM `Tbl_OrdemServiço`";
  • That doesn’t make any difference in this case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.