sql >> Database teknologi >  >> RDS >> Mysql

oprette en mysql søgestreng dynamisk?

Dette er et hurtigt eksempel. Jeg ved ikke, hvilken slags data JRequest::getVar returnerer (altid en streng eller blandede typer?), men dette burde starte dig. Sørg for at bruge den escape-metode, der gælder inden for hver løkke:

if ($post) {
    $criteria = array();
    //get all search variables
    $criteria['type'] = JRequest::getVar('type');
    $criteria['classifications'] = JRequest::getVar('classifications', array(0), 'post', 'array');
    $criteria['rating'] = JRequest::getVar('rating');

    //if there are some criteria, make an array of fieldName=>Value maps
    if(!empty($criteria)) {
        $where = array();
        foreach($criteria as $k => $v) {
            //IMPORTANT!!
            //$v is the value of the field, needs to be quoted correctly!!
            $where[] = "$k = '$v'";
        }
    }
    //create search string
    $query =  "SELECT * FROM #__db_clients";

    if($where) {
        $query .= " where " . join(' AND ', $where);
    }   
} else {    
    echo 'There has been an error, please try again.';
};



  1. node-mysql forbindelse pooling

  2. Postgresql trigger ved brugerlogon

  3. diff 2 store databasetabeller

  4. Best Practices for PostgreSQL-revisionslogning