[solved] {class} function select WHERE LIKE AND

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

[solved] {class} function select WHERE LIKE AND

Post 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 ?
Last edited by ol4pr0 on Thu Dec 09, 2004 2:19 pm, edited 3 times in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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;
}
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

count($result) ?

What is $result?

A mysql resource ?

Then you should use mysql_num_rows($result);
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Well actually no.

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

thanks tmv forget about the debug rules.
Post Reply