user6052525
user6052525

Reputation: 85

php array not showing up in javascript

I created an array of values from data in MySQL. I then try to print it in Javascript but unfortunately it is not showing up. Database has two cols: gurtej and singh

<?php
require_once('config.php');
function readAttribute($connect){
    $resultArray=array();
    $sql="SELECT Attributename,Entityname FROM temp_data_attribute";
    $res=mysqli_query($connect,$sql);
    echo mysqli_num_rows($res);
    if(mysqli_num_rows($res)==0){
        return 0;
    }
    else{
        while($result=mysqli_fetch_array($res)){
            array_push($resultArray,$result[0]);
        }

        return $resultArray;
    }

}
?>
<html>
<body>
<script>
    var jArray= <?php
$resultArray=readAttribute($connect);
if($resultArray==0){
$resultArray=[];
}
echo json_encode($resultArray);?>;
    var counter=<?php echo count($resultArray);?>;
    document.write(jArray[0]);

</script>
<?php
print_r($resultArray);
?>
</body>
</html>

And when i try to print it in php using print_r this is the result.

Array ( [0] => gurtej [1] => singh )

Upvotes: 2

Views: 124

Answers (3)

user2417483
user2417483

Reputation:

Lets clean up the code:

<html>
<body>
<!-- container to hold data -->
<div id='container'></div>
<script>
   <?php 
      //--- get the results from the database
      $resultArray=readAttribute($connect);
      //--- convert if necessary
      if($resultArray==0){
         $resultArray=[];
      }
   ?>;
   //--- place results into JavaScript array
   var jArray= <?php echo json_encode($resultArray);?>;
   //--- show the raw array
   document.getElementById('container').innerHTML = jArray[0];
</script>
<?php
print_r($resultArray);
?>
</body>
</html>

Upvotes: 0

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123453

The resulting JavaScript statement includes more than just the var and array as JSON:

var jArray = 2["gurtej","singh"];

The extra 2 comes from the echo within readAttribute() showing the number of rows:

function readAttribute($connect){
    $resultArray=array();
    $sql="SELECT Attributename,Entityname FROM temp_data_attribute";
    $res=mysqli_query($connect,$sql);
    echo mysqli_num_rows($res);
//  ^^^^

By following the number, the "array" changes meaning and is interpreted as a bracket property accessor with a comma operator. The statement behaves the same as:

var jArray = (2).singh;

And, the number 2 doesn't (normally) have a property named singh. So, you get undefined, which can't have properties.

console.log(typeof jArray); // 'undefined'
console.log(jArray[0]);     // TypeError

To remove the 2, you'll want to remove or comment out the additional echo:

function readAttribute($connect){
    $resultArray=array();
    $sql="SELECT Attributename,Entityname FROM temp_data_attribute";
    $res=mysqli_query($connect,$sql);
    // echo mysqli_num_rows($res);
    // ...
}
var jArray = ["gurtej","singh"];

console.log(typeof jArray); // "object"
console.log(jArray[0]);     // "gurtej"

Upvotes: 1

Machavity
Machavity

Reputation: 31614

Let's clean up that function a bit and we'll fix the problem too

function readAttribute($connect){
    $resultArray = array();
    $sql="SELECT Attributename,Entityname FROM temp_data_attribute";
    $res=mysqli_query($connect,$sql);
    //echo mysqli_num_rows($res);

    while($result = mysqli_fetch_assoc($res)){
        $resultArray[] = $result;
    }

    return $resultArray;
}

Your problem is that you were using $result[0], but that just pushes the first column. And if you're going to json_encode this, I would avoid using fetch_array() (without an argument) because it returns both a numeric AND associative key value for each column (which will inflate your data set of rows)

Upvotes: 0

Related Questions