By lallen30 on
I'm trying to create a php page to connect to my drupal mysql database. I got the following code from the book "Pro Drupal Development" in chapter 5 but I can't get it to print any values.
Can anyone tell me what's wrong with this code:
include_once('includes/bootstrap.inc')
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE)
$sql = "SELECT * FROM {node} WHERE type = 'page' AND status = 1";
$result = db_query(db_rewrite_sql($sql));
while ($data = db_fetch_object($result)) {
$node = node_load($data->nid);
print node_view($node, TRUE);
}
Thanks,
Larry
Comments
Well, right off the bat, you
Well, right off the bat, you have to have page nodes that have been published. You also don't say what results you do get.
There are a few possibilities
Larry,
A few possibilities come to mind.
1. Your page might not allow executable php code. Look in the input format section of your page and see if the radio button for "PHP code" is selected. If not, your code won't run at all.
2. Your site might be configured to disallow PHP (for security reasons.) Look in http://[your-site]/admin/settings/filters to see if "PHP code" is allowed for any role.
3. Your php code might not be finding your settings.php. This happened to me and caused me a fair amount of grief. I ended up having to add this line before the include_once statement: require_once 'settings.php';
This was in a multi-site deployment and, for some reason, Drupal couldn't find my database (which is referenced in the settings.php file) until I pointed it to that file. In my case, the settings.php file I wanted was at the same level as the include directory.
To debug the problem I would first create a page that has just this in it and see if you get any output.
By doing this you'll quickly tell if you're having a problem with your php code or with Drupal running any PHP code. If you get no output then Drupal is probably disallowing execution of php code. If you get a slew of output then the problem is either with your php code or with the Drupal bootstrap process in that php code.
If phpinfo returns output then you could put print statements into your code to narrow the problem. I'd put one right before the "$sql = ..." line to see if the code makes it past the Drupal bootstrap code. If it does then maybe your sql statement isn't returning anything so your while statement has nothing to output. If the code doesn't get as far as your debug print statement then the drupal bootstrap may be dying. I'd try pointing it to your settings.php file in this case and see if that helps.
Let me know if these tips help you to resolve the problem. If not, provide more detail and I'll help as much as I can.
thanks for you reply. I
thanks for you reply. I looked in /admin/settings/filters but I'm only showing html filtered and full html choices, no php check box. I can't find where the option is to enable. Can someone tell me how to enable this.
Thanks,
Larry
You can create a new filter
You can create a new filter by clicking the add input format tab and clicking on the PHP evaluator.
Insert a few var_dump()'s
Insert a few var_dump()'s and print()'s and what do you see?
Like after node_load()? var_dump($node). What is the result?
Or just a print "qwerrtyuyt" in the while{} block just to make sure it's being called.
"I can't get it to print any values." just indicates you have not tried very hard to debug this thing.
Do you have any published nodes?
More information = better. You're not giving much to go on here.
Since none of the code in the
Since none of the code in the book actually works as is, you might try looking here too. http://www.drupalbook.com/errata2?page=2
(It's a really interesting book. However, I think publishing a book with that many errors is a big bunch of BS and I think that they should at least give me some of the $50 USD back that I spent on it. How the hell should I know if I should remember something for not? And, if I did the know the difference why would I want the book anyway?)
revision: in case the publishers want to know the technique.......
(It's a really interesting book. However, I think publishing a book with that many errors is a big bunch of BS and they should at least give me some of the $50 back that I spent on it. How the hell do I know if I should remember something or not? And, if I already know the difference why would I need the book in that case?)
Don't get me wrong. The book has a ton of useful information. But if your going to spend that much time making a book make sure that it's up to date and correct.