0

I wrote code to select records from the database, but it is causing the following error:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM pricepackages pricepackages' at line 1: SELECT FROM {pricepackages} pricepackages; Array ( ) in view_packages() (line 25 of D:\xampp\htdocs \talyllyn\sites\all\modules\pricepackages\pricepackages.module).

What is wrong with the code I am using?

function view_packages() {
    $list = '<table>';
    $query = db_select('pricepackages')->execute();
    $i = 0;

    while ($record = $query->fetchAssoc()) {
        $list .= '<tr><td>';
        $list .= $record['packageName']."</td>";
        $list .= '<td>'.$record['packageDescp']."</td>";
        $list .= '<td>'.$record['pPrice']."</td>";
        $list .= '</tr>';
        $i++;
    }

    $list .= '</table>';
    return $list;
}
  • First comment on drupal api db_select page should provide the answer to this. – user20301 Oct 23 '14 at 9:19
2

This is your current query as it's executed:

SELECT 
FROM 
{pricepackages} pricepackages

Which is obviously wrong; an SQL SELECT query needs one or more fields. To SELECT *:

$query = db_select('pricepackages')->fields('pricepackages')->execute();     

Or specific fields:

$fields = array('foo', 'bar');
$query = db_select('pricepackages')->fields('pricepackages', $fields)->execute();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.