1

Here is my PDO request :

$query = db_select('review_ratings_table')
   ->fields('review_ratings_table',array('review_nid'))
   ->condition('loc_id', $loc_id, 'LIKE')
   ->condition('user_id', $user_id, 'LIKE')
   ->condition('valid', '1', '=')
   ->execute();
while($record = $query->fetchAssoc()) {
 drupal_set_message('<pre>test '.print_r($record).'</pre>');
}

I have tried many ways to get the result of this request (fetchAll, fetchField, fetchAssoc ...) and I keep printing "1" instead of the review_nid or anything else.

Thanks for you help.

Will

=> Hi, If I use dvm($query) I get that :

DatabaseStatementBase::__set_state(array( 'dbh' => DatabaseConnection_mysql::__set_state(array( 'needsCleanup' => false, 'target' => 'default', 'key' => 'default', 'logger' => NULL, 'transactionLayers' => array ( 'drupal_transaction' => 'drupal_transaction', ), 'driverClasses' => array ( 'SelectQuery' => 'SelectQuery', 'DatabaseTransaction' => 'DatabaseTransaction', ), 'statementClass' => 'DatabaseStatementBase', 'transactionSupport' => true, 'transactionalDDLSupport' => false, 'temporaryNameIndex' => 0, 'connectionOptions' => array ( 'database' => '*******', 'username' => '*******', 'password' => '*******', 'host' => 'localhost', 'port' => '3306', 'driver' => 'mysql', 'prefix' => array ( 'default' => '', ), ), 'schema' => NULL, 'prefixes' => array ( 'default' => '', ), 'prefixSearch' => array ( 0 => '{', 1 => '}', ), 'prefixReplace' => array ( 0 => '', 1 => '', ), )), 'queryString' => 'SELECT review_ratings_table.review_nid AS review_nid FROM review_ratings_table review_ratings_table WHERE (loc_id LIKE :db_condition_placeholder_0 ESCAPE \'\\\') AND (user_id = :db_condition_placeholder_1) AND (valid = :db_condition_placeholder_2) ', ))

and

Error message Recoverable fatal error: Object of class DatabaseStatementBase could not be converted to string in review_ratings_table_node_presave() (line 52 of C:\Bitnami\drupal-7\apps\drupal\htdocs\sites\all\modules\custom\review_ratings_table\review_ratings_table.module).

and

dvm($query->fetchAssoc()); gives me that

array( 'review_nid' => '2', )

Which is what I want but I don't know how to access it.

Thanks

8
  • What's in $loc_id and $user_id, and why are you using LIKE for (what I assume is) a numeric field? It'd be a good idea to try your query in raw SQL first to make sure that logic is sound
    – Clive
    May 10, 2015 at 11:33
  • Hi Clive, $loc_id is a string an $user_id an integer. I have tried with a raw SQL and I got the same result unfortunately. I have also tried without any conditions and it was the same. Thanks
    – Will
    May 10, 2015 at 11:38
  • what are the results of dvm($query); and dvm($query->fetchAssoc());? If it doesn't show anything then try with var_dump() also...
    – user12947
    May 10, 2015 at 12:44
  • Hi Jhilke Dai I have added the result of dvm($query); into my original question. I don't know why but var_dump gives me an empty result. thanks
    – Will
    May 10, 2015 at 13:08
  • Following your latest edit, $result = $query->fetchAssoc(); return $result[0]['review_nid'];
    – Ben
    May 10, 2015 at 14:28

1 Answer 1

0

With drupal_set_message('<pre>test '.print_r($record).'</pre>'); you're putting the return value of print_r into a string, not printing the contents of $record.

You can acccess the value you need with:

$result = $query->fetchAssoc();

// make sure we found something
if (empty($result) || empty($result[0]['review_nid'])) {
  dpm("No result");      
}

// load the noad we found
$node = node_load($nid)
dpm($node);
2
  • Hi Ben, what do you mean by "do something with the nid"? I would need be able to display at least for testing purpose. thanks
    – Will
    May 10, 2015 at 15:49
  • Added more code above but, really, as long as you use print_r correctly in your original code you're good to go.
    – Ben
    May 11, 2015 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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