The query below will be used a search script. For some reason it won\'t return all results where either condition is true. What am i doing wrong?
$sql = \"SELE
Your loop overwrites the value of $name
and $code
with each loop, so all you will eventually see is the value of the last cycle.
while( $row = mysql_fetch_array( $query ) ) {
$name = htmlspecialchars($row['name']);
$code = htmlspecialchars($row['id_code']);
}
You can either echo
those values from within your loop, or push them onto a collection someplace:
while ( $row = mysql_fetch_array( $query ) ) {
$names[] = htmlspecialchars( $row["name"] );
$codes[] = htmlspecialchars( $row["id_code"] );
}
Or you could put both values into a single array:
$set = array();
while ( $row = mysql_fetch_array( $query ) ) {
$set[] = array(
"Name" => htmlspecialchars( $row["name"] ),
"Code" => htmlspecialchars( $row["id_code"] )
);
}
At this point, you have loaded all of the names and codes into arrays (or an array) that can be manipulated after your loop has run its course.
print_r( $names ); // or $set
Additionally, you have some redundant code:
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
This runs your query twice - no need for that.
$num_rows1 = mysql_num_rows($result);
$rows = mysql_num_rows($result);
This is counting the number of rows returned, twice. Again, no need for that.