La solicitud de SQL simple está fallando

I try to count the number of roles in my Drupal database and with the result execute another statement. But for some reason, it's failing. What do I do wrong?

$numberofroles = db_query('SELECT COUNT(rid) FROM {role}');
$roles = 1;

while ($roles < $numberofroles) {
        db_query('INSERT INTO {taxonomy_access_default} (vid, rid, grant_view, grant_update, grant_delete, grant_create, grant_list) VALUES(0, '.$roles.', 1, 0, 0, 0, 1)');
        $roles++;
}

preguntado el 08 de noviembre de 11 a las 12:11

I know nothing about drupal and php but is that curly bracket surrounding the table name {role} normal? I would think that it's not needed. -

2 Respuestas

You forget a? ->fetchField() ?

Drupal6:

$val = db_result(db_query({...}))

Drupal7:

$val = db_query({...})->fetchField();

respondido 08 nov., 11:16

I did forget the fetchField()! Thanks! - Michiel

What does db_query return? Your SQL query will return a single column and a single row, but does db_query return an integer or the result set as an array?

print $numberofroles (tip: $numberOfRoles is a lot easier to read) and see what the value is. If it prints as array() then you need to process the array and extract the value you want, likely $numberofroles[0][0]

respondido 08 nov., 11:16

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.