http://drupal.org/node/224333 has two examples incorrect for d7, one refers to db_fetch_array in d7 and one refers to db_fetch_object in d7. These functions don't exist in D7 according to api.drupal.org and return a function not found.

Example 1:


Drupal 6.x:
function example_uninstall() {
$result = db_query("SELECT * FROM {example}");
while ($data = db_fetch_object($result)) {
db_query("DELETE FROM {variable} WHERE name = '%s'", 'example_'. $data->nid);
}
drupal_uninstall_schema('example');
}
Drupal 7.x:
function example_uninstall() {
$result = db_query("SELECT * FROM {example}");
while ($data = db_fetch_object($result)) {
db_query("DELETE FROM {variable} WHERE name = '%s'", 'example_', $data->nid);
}
}

Example 2:


Example - Drupal 6:

$result = db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid));

Drupal 7:

$result = db_fetch_array(db_query("SELECT * FROM {block_custom} WHERE bid = %d", $bid));

Or just do a find on the page for db_fetch

I'm unsure what the correct example (hence why I was hitting this page in the first place, to find the answer for what I should be using in this situation)

Comments

jhodgdon’s picture

Title: Corrections for converting 6.x to 7.x. module pages » 6.x to 7.x. module pages using old DB functions in examples
Project: Documentation » Drupal core
Version: » 7.x-dev
Component: Correction/Clarification » documentation
Issue tags: +Needs documentation updates

Thanks for reporting this! For future reference, we manage the Converting Modules guides in the Drupal Core issue queue, using tag "Needs Update Documentation".

coderintherye’s picture

Great, thank you, I will use that tag in the future.
I've actually got a number of things to file against that page :/, wondering if I should make them all one issue or separate issues?

jhodgdon’s picture

Separate issues for separate issues, if that makes sense. For example, if several places have the same problem, they can be grouped in the same issue (e.g., several examples are using old DB-related functions). But if the problems are unrelated, they should be in separate issues.

Are you interested in fixing any of the issues in the Needs Update Doc queue? Filing the issues is a HUGE help (you can verify yours are not there before you filed them), but fixing them would also be helpful.

You probably don't have permission to edit that page, but if you're interested, and feel you are a good writer of documentation, we can take care of that.

coderintherye’s picture

Awesome, thanks for the response, and yes I am interested. I've done documentation at my previous job, so feel that at the least I won't detract from the page ;)
Given all the 6 -> 7 work I do, it's important for me to get that page in shape, because it's my go to resource almost every day.

droplet’s picture

In D7, default mode is fetch into object (PDO::FETCH_OBJ, equal to db_fetch_object in D6)

db_fetch_array
http://api.drupal.org/api/drupal/includes--database.mysql.inc/function/d...

which return ..

return mysql_fetch_array($result, MYSQL_ASSOC);

it used MYSQL_ASSOC, so turn into D7 and PDO, we do something like:

$result = db_fetch_array(db_query("SELECT * FROM {boxes} WHERE bid = %d", $bid));
$result = db_query("SELECT * FROM {boxes} WHERE bid = :bid", array(':bid' => $bid), array(
  'fetch' => PDO::FETCH_ASSOC,
))->fetch();

more:
http://www.php.net/manual/en/pdostatement.fetch.php

droplet’s picture

maybe this more clean


$result = db_query("SELECT * FROM {boxes} WHERE bid = :bid", array(':bid' => $bid));

$record = $result->fetch();
$record = $result->fetchObject();
$record = $result->fetchAssoc();

jhodgdon’s picture

RE #4 - I thought the page was locked for editing, but looking at the input format I dont' think it is. Do you see the edit link at the top?

coderintherye’s picture

I could have sworn it was before. Considering now that it is not (or that i am now just no longer blind), hopefully I can start making additions/revisions. Thanks.

jhodgdon’s picture

Perhaps someone changed the input format and made that page editable. I thought it was locked before also.

jhodgdon’s picture

Status: Active » Fixed

I believe I have made the necessary changes. There were only two places I saw that used db_fetch_* in D7 examples, and I fixed both of them. If someone could review the changes and verify they are correct, that would be helpful. Meanwhile, marking fixed.
http://drupal.org/node/224333/revisions/view/1482240/1482266

droplet’s picture

it looks good.

Status: Fixed » Closed (fixed)
Issue tags: -Needs documentation updates

Automatically closed -- issue fixed for 2 weeks with no activity.