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($strTabl eName, $arrValue, $arrConditionPairs){
$strSeparator = '';
$strValues = '';
foreach ($arrValue as $strValue) {
$strSetStatements = $strSetStatements.$strSepa rator.$str Value;
$strSeparator = ',';
}
$strSeparator = '';
foreach ($arrConditionPairs as $strCol => $strValue){
$strSelectConditions = $strSelectConditions.$strS eparator.$ strCol.'=' .quote_sma rt($strVal ue);
$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('TableNa me',$arrVa lue,$arrCo nditionPai rs);
// 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>');
Am I going down a dead end?
function standardSQLSelect($strTabl
$strSeparator = '';
$strValues = '';
foreach ($arrValue as $strValue) {
$strSetStatements = $strSetStatements.$strSepa
$strSeparator = ',';
}
$strSeparator = '';
foreach ($arrConditionPairs as $strCol => $strValue){
$strSelectConditions = $strSelectConditions.$strS
$strSeparator = ' AND ';
}
$strSelectConditions = $strSelectConditions;
$sql="SELECT $strSetStatements FROM $strTableName WHERE $strSelectConditions";
$result = mysql_query($sql);
if(!$result) {
die("Query Failed.".$sql.'<p>'.mysql_
}
else {
return $result;
}
}
echo ('<table border="1" cellspacing="2" cellpadding="0">');
//example
$arrValue = array('Col1','Col2', 'Col3');
$arrConditionPairs = array('Col2' => '90','Col3' => 'foobar');
standardSQLSelect('TableNa
// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks