Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

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>');
      
0
MarkMcGhie
Asked:
MarkMcGhie
1 Solution
 
akshah123Commented:
try this ...

//example
$arrValue = array('Col1','Col2', 'Col3');
$arrConditionPairs = array('Col2' => '90','Col3' => 'foobar');

$result = standardSQLSelect('TableName',$arrValue,$arrConditionPairs);  // notice the variable activated here ....
 
 // 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>');
     
0
 
MarkMcGhieAuthor Commented:
Excellent – It worked beautifully!

Thanks
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now