How can i retrieve all the records from a table in SugarCRM?

北慕城南 提交于 2019-12-10 16:48:50

问题


I am using Sugar Pro 6.1 and want to know that how can i retrieve all the products with their ids from the products table. I am trying with the following code

$sql = "SELECT id, name FROM products order by name"; 
$result = $GLOBALS["db"]->query($sql);
$products = $GLOBALS["db"]->fetchByAssoc($result);

but it always returns only the first record.

Is it possible to grab all the products with their ids to display them in a html dropdown, i want to display that drop down in a javascript file thats why i am using the ajax call and in a seperate php file i am using the above code that returns the ouput to the ajax call.

Any help will be appreciated!


回答1:


fetchByAssoc() only grabs one record at a time. Instead, you need to iterate thru calling fetchByAssoc() like this...

$sql = "SELECT id, name FROM products order by name"; 
$result = $GLOBALS["db"]->query($sql);
while ( $product = $GLOBALS["db"]->fetchByAssoc($result) ) {
     $products[] = $product;
}



回答2:


You need to exclude products that are deleted.

$sql = "SELECT id, name FROM products WHERE deleted=0 order by name";
$result = $GLOBALS["db"]->query($sql);
while ( $product = $GLOBALS["db"]->fetchByAssoc($result) ) {
     $products[] = $product;
}



回答3:


Even though i agree with answer posted by Pelish8. But This is also the another way to fetch data form database

global $db;
$sql   =    "SELECT id, name FROM products WHERE deleted=0 order by name";
$result    =    $db->query($sql);
while($product    =    $db->fetchByAssoc($result)){
    $products[] = $product;
}


来源:https://stackoverflow.com/questions/12192010/how-can-i-retrieve-all-the-records-from-a-table-in-sugarcrm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!