mysql - How to declare variables in a query, in a PHP function -


i'm struggling few days query. assume simple, can´t see i'm doing wrong.

i'm using "functions.php" file save queries, each 1 within own php function, like:

$query ='     select *     table '; 

i able use variables in queries, declaring them within query self. like:

$query ='     set @myvar := 0     select id, @myvar := @myvar + somerow result     table '; 

i've been googling , searching in so, found many solutions can't make of them work... sure missing something.

any advise welcome...

thank you

that 2 question (querys) . must separete it.

$query ='     set @myvar := 0;     select id, @myvar := @myvar + somerow result     table; '; 

or init so

$query ='     select id, @myvar := @myvar + somerow result     table, (select @myvar := 0) init '; 

a sample

mariadb [test]> select id, order_id orders limit 10; +-----+----------+ | id  | order_id | +-----+----------+ | 632 |      111 | | 633 |      111 | | 634 |      111 | | 635 |      112 | | 636 |      113 | | 637 |      113 | | 638 |      113 | | 639 |      113 | | 640 |      113 | | 641 |      113 | +-----+----------+ 10 rows in set (0.00 sec)  mariadb [test]> select id, @myvar := @myvar + order_id     -> orders, (select @myvar := 0) init limit 10; +-----+-----------------------------+ | id  | @myvar := @myvar + order_id | +-----+-----------------------------+ | 632 |                         111 | | 633 |                         222 | | 634 |                         333 | | 635 |                         445 | | 636 |                         558 | | 637 |                         671 | | 638 |                         784 | | 639 |                         897 | | 640 |                        1010 | | 641 |                        1123 | +-----+-----------------------------+ 10 rows in set (0.00 sec) 

Comments