简体   繁体   中英

How to write PHP code inside MySQL query

I am confused how to create loop inside MySQL query.

I have this query:

<?php
    $arr = explode(',', $features);
    $query_spicy = "
    SELECT wposts.*             
        FROM $wpdb->posts wposts
    INNER
        JOIN ( SELECT post_id
               FROM $wpdb->postmeta wpostmeta
                    WHERE ('; 
                                ( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test1%' )
                            OR 
                                ( wpostmeta.meta_key = 'features'AND wpostmeta.meta_value LIKE '%test2%' )
                            OR 
                                ( wpostmeta.meta_key = 'features'AND wpostmeta.meta_value LIKE '%test3%' )
                            OR
                                ( wpostmeta.meta_key = 'features'AND wpostmeta.meta_value LIKE '%test4%' )
                            OR
                                ( wpostmeta.meta_key = 'features'AND wpostmeta.meta_value LIKE '%test5%' )
                    ')
                    GROUP BY post_id
            ) AS t ON t.post_id = wposts.ID WHERE wposts.post_status = 'publish' AND wposts.post_type = 'book' ORDER BY wposts.post_date DESC";
?>

And I want generate bellow part dynamic with for loop:

( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test1%' ) OR 
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test2%' ) OR 
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test3%' ) OR
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test4%' ) OR
( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%test5%' )

Note: test1, test2, test3, test4 and test5, I am getting from this $arr .

I hope you understand my question and I will thankful if you help me.

Thanks :)

try something like

$arr = explode(',', $features);
$conditions = array();

foreach($arr as $feature){
    $condition[] = "( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '{$feature}' )";
}
$query = "SELECT ...". join("OR",$conditions);

But you can simply do something like wpostmeta.meta_key = 'feature' and wpostmet IN($features)

I think second solution is better and faster.

Try this

wpostmeta.meta_key = ".$features." AND wpostmeta.meta_value LIKE '%test1%'

<?php
    $arr = explode( ',', $features );

    $query_spicy = "
    SELECT wposts.*             
        FROM {$wpdb->posts wposts}
    INNER
        JOIN ( SELECT post_id
               FROM {$wpdb->postmeta wpostmeta}
                    WHERE (";

    $tmp = array();
    foreach( $arr as $value )
    {
        $tmp[] = "( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%{$value}%' )";
    }

    $query_spicy .= implode( ' OR ', $tmp );
    $query_spicy .= ")
                    GROUP BY post_id
            ) AS t ON t.post_id = wposts.ID WHERE wposts.post_status = 'publish' AND wposts.post_type = 'book' ORDER BY wposts.post_date DESC";
?>

FWIIW:

Just set an array to contain the data you need to insert then use foreach to loop through it.

$sql="SELECT wposts.*             
        FROM $wpdb->posts wposts
    INNER
        JOIN ( SELECT post_id
               FROM $wpdb->postmeta wpostmeta
                    WHERE ('; ";
foreach($arrayoftestdata as $test)
    {
    $sql.="( wpostmeta.meta_key = 'features' AND wpostmeta.meta_value LIKE '%$test%' ) OR ";
    }

$sql.="')
                    GROUP BY post_id
            ) AS t ON t.post_id = wposts.ID WHERE wposts.post_status = 'publish' AND wposts.post_type = 'book' ORDER BY wposts.post_date DESC";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM