Link to home
Start Free TrialLog in
Avatar of MarkMcGhie
MarkMcGhie

asked on

Standardized MySQL Select function

I altered a function found on php.net (http://www.php.net/manual/en/function.mysql-query.php#62227) to create a generic select function.  The query appears to work (no errors), but I can’t get the results to return.  The  call to mysql_fetch_array produces the error “supplied argument is not a valid…”.

Am I going down a dead end?


   function standardSQLSelect($strTableName, $arrValue, $arrConditionPairs){
       $strSeparator = '';
       $strValues = '';
       foreach ($arrValue as $strValue) {
           $strSetStatements = $strSetStatements.$strSeparator.$strValue;
           $strSeparator = ',';
       }
       $strSeparator = '';
       foreach ($arrConditionPairs as $strCol => $strValue){
           $strSelectConditions = $strSelectConditions.$strSeparator.$strCol.'='.quote_smart($strValue);
           $strSeparator = ' AND ';
       }
       $strSelectConditions = $strSelectConditions;
        $sql="SELECT $strSetStatements FROM $strTableName  WHERE $strSelectConditions";
        $result = mysql_query($sql);
        if(!$result) {
        die("Query Failed.".$sql.'<p>'.mysql_error());
        }
        else {
        return $result;
      }
   }
   
 echo ('<table  border="1" cellspacing="2" cellpadding="0">');
   
//example
$arrValue = array('Col1','Col2', 'Col3');
$arrConditionPairs = array('Col2' => '90','Col3' => 'foobar');

standardSQLSelect('TableName',$arrValue,$arrConditionPairs);
 
 // fetch associative array
 while($row = mysql_fetch_array($result) ) {
         $Col1 = $row['Col1'];
       $Col2 = $row['Col2'];
       $Col3 = $row['Col3'];
 
       
echo ('      <tr>
                  <td>Column 1</td>
                  <td>Column 2</td>
                  <td>Column 3</td>
            </tr>
            <tr>
                  <td>'.$Col1.'</td>
                  <td>'.$Col2.'</td>
                  <td>'.$Col3.'</td>
                  
            </tr>')      ;
      }
echo('</table>');
      
ASKER CERTIFIED SOLUTION
Avatar of akshah123
akshah123
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MarkMcGhie
MarkMcGhie

ASKER

Excellent – It worked beautifully!

Thanks