How to create a complex query with concatenations with RB

27 views
Skip to first unread message

Reka

unread,
Jan 10, 2017, 2:54:07 PM1/10/17
to redbeanphp

Hello,

I try to translate into Redbean syntax that kind of queries :

$sql = "SELECT * FROM questions AS Q WHERE trash = '' ";
           
if ($type_thematic != "")
                $sql
.= "AND EXISTS (SELECT id FROM question_cat1 AS QT WHERE id_cat1=$cat1 AND Q.id = QT.id_question) ";
           
if ($produits_thematic != "")
                $sql
.= "AND EXISTS (SELECT id FROM question_cat2 AS QP WHERE id_cat2=$cat2 AND Q.id = QP.id_question) ";
           
if ($autres_thematic != "")
                $sql
.= "AND EXISTS (SELECT id FROM question_cat3 AS QA WHERE id_cat3=$cat3 AND Q.id = QA.id_question) ";
           
if ($demandeur_thematic != "")
                $sql
.= "AND EXISTS (SELECT id FROM question_cat4 AS QD WHERE id_cat4=$cat4 AND Q.id = QD.id_question)";

            $sql
.= " ORDER by id DESC";

           
if ($query != "all")
                $sql
.= " LIMIT 25";

            $result
= mysqli_query($connect, $sql) or die ("Forbidden");
       
}


How can I make it, please ?

I really appreciate any help you can provide.

Regards,

Reka

gabor

unread,
Jan 11, 2017, 5:19:45 AM1/11/17
to redbeanphp

Hi there,

RedBeanPHP does not have a query builder so the answer is as simple as:

R::getAll( $sql ); instead of mysqli_query( $connect, $sql );

that's all

http://www.redbeanphp.com/index.php?p=/query_builder

cheers
gabor

Reka

unread,
Jan 14, 2017, 6:55:32 PM1/14/17
to redbeanphp
Ok, thank you Gabor, I understand !
My error was to construct my query upside down :
If I create a SQL query, affect it to a variable and then call the result with getAll($sql) at the end, it works ;)

like this example (http://www.redbeanphp.com/index.php?p=/querying),
$sql = 'SELECT author.* FROM author
        JOIN club WHERE club.id = 7 '
;
$rows
= R::getAll( $sql );

Cheers,

Reka
Reply all
Reply to author
Forward
0 new messages