Page 1 of 1

[solved] {class} function select WHERE LIKE AND

Posted: Thu Dec 09, 2004 1:59 am
by ol4pr0

Code: Select all

function select_like( $table, $condition="", $sort="" ) {
        $query = "SELECT * FROM $table";
        $query .= $this->_makeWhereLike( $condition );
         ...........
         ...........ect..

    function _makeWhereLike( $condition ) {
        if ( empty( $condition ) )
            return "";
        $retstr = " WHERE ";
        if ( is_array( $condition ) ) {
            $cond_pairs=array();
            foreach( $condition as $field=>$val )
                array_push( $cond_pairs, "$field LIKE %".$this->_quote_val( $val )."%" );
            $retstr .= implode( " AND ", $cond_pairs );
        } elseif ( is_string( $condition ) && ! empty( $condition ) )
            $retstr .= $condition;
        return $retstr;
    } 


#calling it. *(trying hehe)
# column1 is for the LIKE and column2 is for the AND whatever ej:(age<8)
$result = $dl->select_like("table", array('column1'=>"value1",'column2'=>"value2") )
count($result); returns 1 but i do not believe that.
as when doing a normal $field= & AND it will return one aswell. and the foreach returns the information.

however with the LIKE i get this back
Invalid argument supplied for foreach() in

Code: Select all

foreach( $result as $Rrow ) {
echo $Rrow['column'];
}
Where did i go wrong ?

Posted: Thu Dec 09, 2004 2:37 am
by timvw
basic debugging 101:

Code: Select all

error_reporting(E_ALL);
find out what the query really is , before performing it

Code: Select all

echo $query;
let your rdbms tell what went wrong, for example:

Code: Select all

$resultset = mysql_query($query) or die(mysql_error());
show us how you built the $result array, for example:

Code: Select all

$result = array();
while ($row = mysql_fetch_assoc($resultset))
{
    $result[] = $row;
}

Posted: Thu Dec 09, 2004 4:58 am
by []InTeR[]
count($result) ?

What is $result?

A mysql resource ?

Then you should use mysql_num_rows($result);

Posted: Thu Dec 09, 2004 2:17 pm
by ol4pr0
Well actually no.

count($result) will return the number of matched rows :)

thanks tmv forget about the debug rules.