0

I am having issues with a $json variable in my code. For some reason, my php query isn't being encoded into my $json variable.

$sql = "SELECT `$column`, `Year`, `Month` FROM unemployed WHERE year BETWEEN ? AND ? and month= ?";

$stmt = $conn->stmt_init();
$stmt->prepare($sql); 
$stmt->bind_param('sss', $gYear, $gYear2, $gMonth);
$stmt->bind_result($Column, $year, $month);
$stmt->execute();
$stmt->store_result();
$numRows = $stmt->num_rows;
$json = array();

if ($numRows) { ?>

<table>
<tr>
<th scope="col"> Hi </th>
<th scope="col"> Year </th>
<th scope="col"> Month </th>
</tr>

<?php while ($row = $stmt->fetch()) { ?>
<tr>
<td> <?php echo $Column; ?> </td>
<td> <?php echo $year; ?> </td>
<td> <?php echo $month; ?> </td>
</tr> 
$json['unemployed'][]= $row;
<?php } ?>
</table>
<?php
echo json_encode($json);
var_dump($json);
?>

After running this script, there still isn't anything in my $json variable. Does anyone know why that is? Shouldn't the variable now be an array with my query values (each row would now be part of an array)?

1
  • This is what I get: $json['unemployed'][]= $row; $json['unemployed'][]= $row; echo json_encode($json); var_dump($json); Commented Oct 15, 2013 at 16:39

2 Answers 2

1

It doesn't even look like

$json['unemployed'][]= $row;

is within php tags.

Also, it looks like fetch() returns boolean. When you call fetch(), it looks like it fills the variables $Column, $year, and $month with the values from the row. So instead of

$json['unemployed'][]= $row;

Do this:

$json['unemployed'][]= array($Column, $year, $month);
Sign up to request clarification or add additional context in comments.

9 Comments

Yes that was previous error. Except now I don't believe what I am getting is correct. Here is what var_dump is now: {"unemployed":[true]}array(1) { ["unemployed"]=> array(1) { [0]=> bool(true) } }
@user2562125 What output did you expect?
Something like {"unemployed":[["183,000","1992","January"],["175,000","1993","January"]]}
php.net/manual/en/mysqli-stmt.fetch.php It looks like this function returns True/false
So what could I use in place of $stmt->fetch?
|
0

The issue is that you're outside of the scope of the PHP interpreter when attempting to parse the PHP code.

</tr> 
$json['unemployed'][]= $row;

to

</tr> 
<?php $json['unemployed'][]= $row; ?>

1 Comment

Okay now I am getting this: (var_dump is this) {"unemployed":[true]}array(1) { ["unemployed"]=> array(1) { [0]=> bool(true) } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.