You can do this with PDO or MySQLi , your current version of MySQL is DEPRECATED and strongly advised not to use it, instead look into MySQLi and PDO which have methods in place for exactly what you want to do,
I will run a very quick example, but I think you'll need to read up on the differences between MySQL and MySQLi (there are many websites with good advise, please google)
Your MySQLi would be best in the form of Object orientated and you would have a MySQLi object which contains your connection data:
Mysqli object (here called Dbx())
Dbx() {
private $dbiUser = username;
private $dbiPass = password;
private $dbiName = database name;
public function __construct(){
$this->dbiLink = new mysqli("localhost", $this->dbiUser, $this->dbiPass, $this->dbiName);
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
if ( ! $this->dbiLink )
{
die("Connection Error (" . mysqli_connect_errno() . ") "
. mysqli_connect_error());
mysqli_close($this->dbiLink);
}
else
{
$this->dbiLink->set_charset("latin1");
}
return true;
}
}
public function fetchResult($result)
{
/***
* For use with MySQLi->dbiLink Object.
* Returns the result as an array of KEY=>VALUE pairs from the Database.
**/
$resultsArray = array();
if ($result instanceof mysqli_stmt) {
$result->store_result();
$variables = array();
$data = array();
$meta = $result->result_metadata();
while ($field = $meta->fetch_field()) {
$variables[] = & $data[$field->name]; // pass by reference
}
call_user_func_array(array($result, 'bind_result'), $variables);
$i = 0;
while ($result->fetch()) {
$resultsArray[$i] = array();
foreach ($data as $k => $v)
$resultsArray[$i][$k] = $v;
$i++;
}
} elseif ($result instanceof mysqli_result) {
while ($row = $result->fetch_assoc()) {
$resultsArray[] = $row;
}
}
unset($row);
return $resultsArray;
}
Now, in your PHP code page you first need to call the class file above:
require "includes/classes/dbx.class.inc.php";
Then you need to initiate the new object:
$database = new Dbx();
Now you can use $database as your MySQLi connection to your database.
First, set your 5 parameters as an array (as mentioned by Codrutz codrutz) thus:
$paramaters[0][] = $param1;
$paramaters[0][] = $param2;
$paramaters[0][] = $param3;
$paramaters[0][] = $param4;
$paramaters[0][] = $param5;
...
$paramaters[1][] = $param12;
$paramaters[1][] = $param22;
$paramaters[1][] = $param32;
$paramaters[1][] = $param42;
$paramaters[1][] = $param52;
etc.
Now you have all your parameters in $parameters, you can edit your original query to look through the five (or however many) parameter rows given.
NOTE: that data is not directly inserted into the SQL but applied vai the ? character on MySQL, this is slightly different from PDO which uses a ::variablename structure.
foreach ($parameter as $paramRow){
$sql = "SELECT *, ROUND(InvoiceNo, 0) AS SnPf, RIGHT(InvoiceNo, 3) AS SnSf FROM table WHERE Unit = ? AND CustomerName = ? AND Component = ? AND Site = ? ORDER BY SnPf DESC, SnSf ASC LIMIT 8";
$sqlQuery = $database->prepare($sql);
Prepare the statement, above. The first part of the bind_param is the type of data that is being bound - (s)tring, (i)nteger, (d)double [numeric] or (b)lob [file/big chunks].
This binds the values in the inner array to the ? in the SQL query.
$sqlQuery->bind_param("ssss",$paramRow[0],$paramRow[1],$paramRow[2],$paramRow[3]);
$sqlQuery->execute();
$result[] = $database->fetchresult($sqlQuery); ///fetches the results using the custom method, above.
$sqlQuery->close();
}
//end the foreach loop.
So this will now have a name value array of results from the $result array, each initial row will correspond to each initial row of the $parameters query. To reverse the array as you want you can still array_reverse but the data is selected by name, and should be referenced as such.
Some qualifiers:
I didn't expect this answer to get this big and PDO is much more flexible than MySQLi in handling table outputs. I also realise that much of this may be a bit "WOW,wtf?" to you, but don't worry, read up elsewhere and it should all fit nearly into place. I think to replace everything I've written with the following summary for MySQLi/PDO
$parameters = array of values(5) of values(4)
foreach ($parameter as $row){
Apply row array values to the SQL statement
Get the result
Arrange the result
Save the result to an array
}
Your output will be an array of the form
$result[parameter][row per parameter][table data from row]
Apologies again this has turned into a massive answer.