Passing variable in php

The variable $q=$_GET["q"]; is passing correctly the value "q" to the $query and echoing the $row['YEAR'].

HOWEVER, it is not working with the while statement IN THE SAME CODE.

while ($rows = $result2->fetch_array())

{
$row0 [] = $rows['YEAR'];            
$row1 [] = $rows['MONTH'];        
$row2 [] = $rows['SALES'];
} 

IF I try to type directly the value after the $_GET it does work ( $q=2013; for example )

Can anyone give me a help to make the while work with $q=$_GET["q"];

Bellow is the complete code.

Appreciate any help

<?php
$q=$_GET["q"];

$mysqli = new mysqli('localhost','user','pswd','database');


/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit(); 
}

$query = "SELECT * FROM table_test WHERE `YEAR`='".$q."'";

$result = $mysqli->query($query);

if ($row = $result->fetch_array())

{
echo "<td>" . $row['YEAR'] . "</td>";

}   

$result2 = $mysqli->query($query);

while ($rows = $result2->fetch_array())

{
$row0 [] = $rows['YEAR'];           
$row1 [] = $rows['MONTH'];      
 $row2 [] = $rows['SALES'];
 }  



/* free result set */
$result->close();


 $mysqli->close();

 ?>

Upvotes: 1

Views: 71

Answers (1)

Mark
Mark

Reputation: 8431

$q=$_GET["q"]; will return null if you don't have submit a form with method='get' or the URL of you page does not contain a query string with ?q=2013 like www.example.com?q=2013.

Upvotes: 1

Related Questions