0

I am using eclipse editor. I am programming within vtiger 5.4. in my file config.inc.php the variable $default_charset is setted as

$default_charset = 'UTF-8';

I'm trying to make a sql query in mysql using the next variable

$sql = "select cod_dpto from vtiger_ubi where dpto='" . $dpto . "'";

When I print the variable $dpto I get "SAÑA", but the execution of the query mysql

$adb->query ( $sql );

doesn't work. But when I modify my query as:

$sql = "select cod_dpto from vtiger_ubi where dpto='SAÑA'";

the instruction

$adb->query ( $sql );

returns the values that I need.

Could you help me please, how can I convert my variable $dpto such that the sql query works well.

EDIT

I trying to make the query with the below code, without vtiger, and I get 0 results for thw two cases with variable and writing 'SAÑA'

$servername = "localhost";
$username = "root";
$password = "peru2006";
$dbname = "consuladoperurio_com_br_2";
$port = "3306";

// Create connection
$conn = new mysqli ( $servername, $username, $password, $dbname, $port );
// Check connection
if ($conn->connect_error) {
die ( "Connection failed: " . $conn->connect_error );
}
$sql = "select cod_dpto from vtiger_ubigeo where dpto='$dpto'";
echo $sql;
$result = $conn->query ( $sql );
if ($result->num_rows > 0) {
// output data of each row
while ( $row = $result->fetch_assoc () ) {
    echo "id: " . $row ["cod_dpto"] "<br>";
}
} else {
echo "0 results";
}
$conn->close ();
Juan
  • 2,073
  • 3
  • 22
  • 37
  • Where is the data in variable `$dpto` coming from? POST? Database? – ajmedway Sep 24 '15 at 14:33
  • What does `mb_detect_encoding($dpto);` return? – pbond Sep 24 '15 at 14:36
  • @Juan try `utf8_encode($dpto)` before inserting it into the query. – pbond Sep 24 '15 at 14:40
  • @Juan `iconv('ASCII', 'UTF-8//IGNORE', $dpto);` appears to be a better option I just found out: http://stackoverflow.com/questions/4983989/convert-ascii-to-utf-8-encoding – pbond Sep 24 '15 at 14:41
  • @ajmedway $dpto is filled with a query. – Juan Sep 24 '15 at 14:43
  • @Juan what is the collation/encoding of the database table? – ajmedway Sep 24 '15 at 14:45
  • @pbond I had tested that, that doesn't work. – Juan Sep 24 '15 at 14:45
  • @Juan probaly $dpto got from database, and it will fail on next query because actual value of $dpto is not unicode (maybe cause is by your database configuaration). The solution is you must check the data which $dpto got from and tell us. – Linh Sep 24 '15 at 14:48
  • `if (!$adb->query($sql)) { printf("Errormessage: %s\n", $adb->error); }` run this and tell us the error – ajmedway Sep 24 '15 at 14:50
  • @ajmedway I run that but I don't get any error. but when I execute, after, $adb->fetch_array ( $adb->query($sql) ); I don't get any data. – Juan Sep 24 '15 at 14:56
  • @Juan, right so no error, fine. Please check your database collation and check the specific table collation/encoding and let us know – ajmedway Sep 24 '15 at 14:57
  • @ajmedway table collation utf8_general_ci – Juan Sep 24 '15 at 15:02
  • @Juan that should be fine... please update the question with the code that builds the `$dpto` var including the MySQL select statement – ajmedway Sep 24 '15 at 15:05
  • @ajmedway I had edited my question. – Juan Sep 24 '15 at 16:27
  • @Juan until you show the code of where the data in variable `$dpto` is coming from, I cannot help any further – ajmedway Sep 24 '15 at 16:31
  • @Juan i.e. where `$dpto = x` occurs, and showing where x comes from – ajmedway Sep 24 '15 at 16:36

1 Answers1

-1

Your Select statement looks like this:

$sql = "select cod_dpto from vtiger_ubi where dpto='".SAÑA."';

you'll probably want it to look like:

$sql = "select cod_dpto from vtiger_ubi where dpto='$dpto'";

Notice no concat operator, and the variable is only wrapped in single quotes.

Kisaragi
  • 2,198
  • 3
  • 16
  • 28