tags:

views:

97

answers:

5

The following code throws an error in PHP 5.2.9 after submission, but not on the original load. Error: Fatal error: Only variables can be passed by reference in /home/golfcom/public_html/test.php on line 12

File:

<?php
include('connection.php');
$result = $dbc->query("SELECT subdivision FROM Residential");
$search['subdivision'] = array();

while($i = $result->fetch_array()){
    echo $i['subdivision'];
    array_push($search['subdivision'], $y = $i['subdivision']);
}
?>
<form action="test.php" method="post">
<input type='submit' value='search' class='submit' name='search' /></form>
A: 

Ignore this question, I'm getting inconsistent results. I think my host is wonky.

Jack
A: 

I'm having the same problem:( -Alex

If your host is wonky, why not just switch hosts? :-)
MaxVT
+1  A: 

Just a guess, but maybe it has something to do with the assignment operation inside the function:

array_push($search['subdivision'], $y = $i['subdivision']);

Try this instead:

$y = $i['subdivision'];
array_push($search['subdivision'], $y);
Swish
A: 

You have no error control. You need to check the following:

  • your database connection is OK and connected
  • the query did not error and returned a valid result (even if there are no rows)
  • dont try to print a row if there was none returned by the query

In your loop, $y will be the value of the LAST row and is set every loop. While its not incorrect, you can change your code to something like:

$t = $result->numRows();
while ($row = $result->fetch_array()) {
    $search['subdivision'][] = $row['subdivision'];
}
$lastResult = $search['subdivision'][$t - 1];
OIS
+1  A: 

Figured it out. My host had register_globals on. Turning if off fixed everything.

Jack