0

I am trying to send some data from JS to PHP and make a DB request, which goes back into my JS. Problem is, I only get "null" as result. I checked and double checked my Query which is perfectly fine. Here the JS:

var selectedEngin = getSelectedText(enginID);
selectedEngin = selectedEngin.slice(0, -1);
var roleNameList = new Array();
$.ajax({
     url: '/getRoleNames.php',
     type: "POST",
     dataType:'json',
     data: { engin : selectedEngin },
     success: function(data){
         console.info(data);
     }
});

And the PHP:

include_once ("config.php");

$userAnswer = $_POST['engin']; 
$row = array();

$query="select ROLE_NAME from type_vehicule_role WHERE TV_CODE 
='".$userAnswer."' ORDER BY ROLE_ID" ;
$result=mysqli_query($dbc,$query);
$row=mysqli_fetch_array($result);
echo json_encode($row);

If I give back "test" instead of $row, it works perfectly as well.

Hope you can help me! Thank you!

Have a nice day!

M. Ebner
  • 33
  • 5
  • 3
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard May 16 '17 at 15:47
  • I think you need to loop through `$row` and push each row into an array and then return that array as the response. – Mohit Bhardwaj May 16 '17 at 15:50
  • If you are ORDERing BY then we have to assume your query returns more than one row. However you are only processing one row from the resultset? – RiggsFolly May 16 '17 at 15:51
  • @JayBlanchard Yes, I have watched it in the browser's developer tool and the result is "null". Yes, the jQuery library is included. As I said if $row is replaced by some text it works. No errors and I run it on my XAMPP. – M. Ebner May 16 '17 at 15:51
  • 2
    Is there in fact a matching record in your database? You don't check before trying to encode the returned row. – James May 16 '17 at 15:51
  • @MohitBhardwaj, you mean like: `$row=mysqli_fetch_array($result); while ($row=@mysqli_fetch_array($result)) { array_push($enginRoles, $row[0]); } echo json_encode($row);` Allready tryed .... same result – M. Ebner May 16 '17 at 15:54
  • yes something like that. But you need to use `echo json_encode($enginRoles);` then. – Mohit Bhardwaj May 16 '17 at 15:55
  • @RiggsFolly, I am sure that there are at least 5 rows, I think you suggest the same as Mohit B.? – M. Ebner May 16 '17 at 15:56
  • @MohitBhardwaj, yeah that is what I did, sorry forgot to change it in comment :P – M. Ebner May 16 '17 at 15:58
  • Have you checked that the query actually returns records? – Mohit Bhardwaj May 16 '17 at 15:59
  • Yep, I logged my query (which came back to JS) and ran my query through my DB which gave me the response I was hoping for. – M. Ebner May 16 '17 at 16:02
  • Does `config.php` assign `$dbc` to a valid MySQL DB connection? It's not declared in your sample code. – BA_Webimax May 16 '17 at 16:23
  • @BA_Webimax yes it does, it is declared in the config.php – M. Ebner May 16 '17 at 16:32
  • What is the output of `echo $result->num_rows;` – BA_Webimax May 16 '17 at 16:41
  • The result is: "6", matches with the MYSQL response. Feel like we are getting closer?! – M. Ebner May 16 '17 at 16:43
  • mysqli_result::fetch_fields works as well – M. Ebner May 16 '17 at 17:02
  • @M.Ebner Is your data in UTF8 format or a different encoding? – BA_Webimax May 17 '17 at 13:04
  • @BA_Webimax Found the problem. As you mentioned, it is some kind of encoding problem, but I do not really know where the problem is yet. The Database is in latin_general_cs, the data table is in utf8_general_ci. My php is in Cp1252. Great combo, good that the real server is not windows but linux! I try to find the right encoding for all and keep you guys posted with the full answer. Thanks! – M. Ebner May 18 '17 at 15:34

3 Answers3

0

$dbc has to contain the info with the parameters to connect to the DB. In your case the $result is null as your connection cause an error.

A. Piks
  • 1
  • 3
0

Try using this to get all of the data properly encoded prior to feeding it to the json_encode() call.

include_once ("config.php");

$userAnswer = $_POST['engin']; 
$row = array();

$query="select ROLE_NAME from type_vehicule_role WHERE TV_CODE 
='".$userAnswer."' ORDER BY ROLE_ID" ;
$result=mysqli_query($dbc,$query);
$row=mysqli_fetch_array($result);

// Walk the array and encode everything in UTF8
$utf8Row = array_map(utf8_encode, $row);
// JSONify the encoded array
echo json_encode($utf8Row);
BA_Webimax
  • 2,714
  • 1
  • 13
  • 15
  • works as well as `mysqli_set_charset($dbc,"utf8");` Thanks again! Upvote (less than 15 reputation so not shown)... – M. Ebner May 18 '17 at 15:53
0

Thanks to BA_Webimax I found the problem and got an answer:

The problem was the charset. I solved it using mysqli_set_charset($dbc,"utf8"); in the PHP.

Thank you for your help guys! Have a nice day!

M. Ebner
  • 33
  • 5