RedBeanPHP

easy ORM for PHP

Queries

If you prefer rows rather than beans you need to execute an SQL query, you can use the query functions of RedBeanPHP. To just execute a query:


R
::exec'update page set title="test" where id=1' );

To get a multidimensional array:


R
::getAll'select * from page' );

The result of such a query will be a multidimensional array:


Array
(
    [
0] => Array
        (
            [
id] => 1
            
[title] => frontpage
            
[text] => hello
        
)

    [
1] => Array
        (
            [
id] => 2
            
[title] => back
            
[text] => bye
        
)
)

Note that you can use parameter bindings here as well:


R
::getAll'select * from page where 
title = :title'

array(
':title'=>'home') );

To fetch a single row:


R
::getRow('select * from page where title like ? limit 1',array('%Jazz%'));

The resulting array corresponds to one single row in the result set. For instance:


Array
(
    [
id] => 1
    
[title] => The Jazz Club
    
[text] => hello
)

To fetch a single column:


R
::getCol('select title from page');

The result array of this query will look like this:


Array
(
    [
0] => frontpage
    
[1] => The Jazz Club
    
[2] => back
)

And finally, a single cell...


R
::getCell('select title from page limit 1');

To get an assoc. array with a specified key and value column use:


R
::$adapter->getAssoc('select id,title from page');

result:


Array
(
    [
1] => frontpage
    
[2] => The Jazz Club
    
[3] => back
)


User contributed notes. Please use the comment section to provide tips, notes and examples. To ask for support or to provide feedback use the forum. For bug reports use Github Issue Tracker.

Site comments powered by Disqus

page generated in 0.010607957839966 sec.