1

Im working with mysql and php with angular this is the code for the posting data

app.controller('buscar', function ($scope, $http) {

$scope.postData = function () {

var request = $http({
    method: "POST",
    url: 'busqueda.php',
    data: {
        cedula:$scope.cedula
    },
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
}).then(function successCallback(response) { 
    $scope.datos = response.data;
    console.log($scope.datos);
},function(response) {
  $scope.error= response.statusText;
  console.log($scope.error);
});
}
});

and my php is:

$sql1 = "(SELECT * FROM `presentadas`  LEFT JOIN `desembolsos` ON  presentadas.`CLI_IDENTIFICACION` = desembolsos.`IDENTIFICACION` WHERE presentadas.`CLI_IDENTIFICACION` = '".$cedula."')";
             $result1 = $conn1->query($sql1);

                if ($result1->num_rows > 0) {

                while($row1 = $result1->fetch_assoc()) {
                        $datos1[]=$row1;
                    }
                 echo json_encode($datos1);  
               }else{
                echo "No se encontraron datos en presentadas";
               } 

I'm posting one number called cedula which is the think I'm searching in the DB and it`s corresponding row, as I said, it works with some numbers listed in the DB but with others numbers it doesn't, looking in the chrome's developers console it shows nothing when I showing the response with console.log(). Image 1: here is when is failing, clicking in the console log record, it shows me error in line console.log($scope.datos) I guess it is not getting data

Image 2:Here is a working example showing the json response

I don't know why I'm getting this type of inconsistency. Help me please!!!!

  • 1
    Should send json for both responses – charlietfl Jan 10 '18 at 02:06
  • When you run the sql query with the number you are sending in in `image1`, does it return any rows? – JohanP Jan 10 '18 at 02:12
  • Are you getting more then one value? – MadeInDreams Jan 10 '18 at 02:57
  • Try incrementing the position in the array. $i = 0; if(mysqli_num_rows($result) > 0){ while($row = mysqli_fetch_assoc($result)){ $data[1][$i] = $row; $i++; } – MadeInDreams Jan 10 '18 at 03:01
  • @JohanP in the image 1, with that number I posted it doesn't work it returns nothing just a link to a line in the code and it is the console.log=($scope.datos), but in the image 2 that number I posted works perfectly as you can see the table is fill with the data – camilo mancilla Jan 10 '18 at 03:14
  • If you take that number from image 1 and actually run the query on your db, does it return any rows? I.e in your db, do you expect to have any rows for that number? – JohanP Jan 10 '18 at 03:15
  • @MadeInDreams with that number I posted I don't get any value, but if I try with another number it works, as you see in the image 2 – camilo mancilla Jan 10 '18 at 03:16
  • @JohanP yeah it works, so it's really frustrating cause I have no error in php or Mysql so I think it's a angular issue – camilo mancilla Jan 10 '18 at 03:17
  • Can you try data {'cedula':$scope.cedula} add the quote – MadeInDreams Jan 10 '18 at 03:35
  • @MadeInDreams I noticed why its not wroking, it seems like when there is a special character on the row like(ñ,Ñ,í,etc) its not returning anything, I did a var_dump($datos1) and I can see the array is coming full but in the case of Ñ is raplaced with � – camilo mancilla Jan 10 '18 at 17:44
  • You have to set encoding in your page and with the database as well Failry simple – MadeInDreams Jan 10 '18 at 17:50

2 Answers2

0

Re: Ñ is raplaced with �

Search for 'black diamond' in Trouble with UTF-8 characters; what I see is not what I stored

It says that either you did not start with utf-8 or that the connection was not made to declare that the client has utf-8 bytes. Ñ in latin1 is hex D1. In utf8, it is C391.

Rick James
  • 135,179
  • 13
  • 127
  • 222
0

I solved it setting this line in my code before the query $mysqli->set_charset("utf8") thank you