query + loop problem
| From: | Russ Michell | Date: | Thu, 01 Jan 1970 00:00:00 +0000 |
| Subject: | query + loop problem | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-14654@lists.php.net to get a copy of this message | ||
Hi there everyone:
I have a generic query containing variables, substituted for values dependant upon specific
circumstances:
//----query the DB on the basis of searches employing text input fields (LIKE)
if($sql_clause == 'LIKE') {
$sql = "SELECT * FROM $table WHERE $field $sql_clause '%$query%'";
}
//----query the DB on the basis of searches employing select menus ('=')
else if($sql_clause == '=') {
$sql = "SELECT * FROM $table WHERE $field $sql_clause '$query'";
}
$result = mysql_query($sql,$connect) or printerror("Query Failure!","06");
$numrows = mysql_num_rows($result);
Instead of the usual 'hard-coded' method within the loop as below:
//----loop using hard-coded variables
while($row = mysql_fetch_array($result)) {
$field1 = $row[fieldname1];
$field2 = $row[fieldname2]; //----etc
}//----end while loop
I'd like to be able to have this dynamically enabled as below:
//----loop using relevant field names to display correct page content
while($row = mysql_fetch_array($result)) {
//----generic method of extracting rownames according to '$table' (above) goes here
}//----end loop
Does anyone have a clue how to do this???
Many Thanks!
Russ
#-------------------------------------------------------#