简单的 SQL 请求失败
我尝试计算 Drupal 数据库中的角色数量,并根据结果执行另一条语句。但由于某种原因,它失败了。我做错了什么?
$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++;
}
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++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你忘记了一个? -> fetchField() ?
Drupal6:
$val = db_result(db_query({...}))
Drupal7:
$val = db_query({...})->fetchField();
You forget a? ->fetchField() ?
Drupal6:
$val = db_result(db_query({...}))
Drupal7:
$val = db_query({...})->fetchField();
db_query 返回什么?您的 SQL 查询将返回单列和单行,但 db_query 是否返回整数或结果集作为数组?
print $numberofroles (提示:$numberOfRoles 更容易阅读)并查看值是什么。如果它打印为
array()
那么您需要处理该数组并提取所需的值,可能是$numberofroles[0][0]
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]