I have a problem with json encode of an array of variables $name0 and $name1 If I use a simple echo $name0 and echo $name, I get the correct result for both variables. But if I put $name0 and $name1 in an array $arr and then encode the array with echo json_encode($arr), in my browser I get the value of $name0. But the value of $name1 is "null". How can I get both values right ($name0 and $name1)? Thank you for your help. I am really stuck.
PHP
$sql= "SELECT * FROM names WHERE userId= '$userId' AND number = 0";
$rs =mysql_query($sql, $connexionLogin) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$total_rs = mysql_num_rows($rs);
$totalName0 = $total_rs;
if ($total_rs>0) {
$name0 = $row_rs['name'];
}else if ($total_rs==0) {
$name0 = "empty";
}
echo $name0 //returns "This is the first name"
$sql= "SELECT * FROM names WHERE userId = '$userId' AND number= 1";
$rs =mysql_query($sql, $connexionLogin) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$total_rs = mysql_num_rows($rs);
$totalName1 = $total_rs;
if ($total_rs!=0) {
$name1 = $row_rs['nom'];
}else if ($total_rs==0) {
$name1 = "empty";
}
echo $name1; // returns "This is the second name"
$arr = array(
'name0' => $name0,
'name1' => $name1
);
echo json_encode($arr); //returns
This is the first nameThis is the second name{"name0":"This is the first name","name1":null}