Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the example xpdo.query #408

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions en/extending-modx/xpdo/class-reference/xpdo/xpdo.query.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ The SQL statement to prepare and execute. Data inside the query should be [prope

```php
$result = $modx->query("SELECT * FROM modx_users WHERE id=1");
if (!is_object($result)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe $modx->query will return false if the query is somehow invalid, perhaps also if there are issues connecting to the database. So while that shouldn't happen in this particular example, it is important to check the $result is an object. IIRC it's a \PDOStatement object.

return 'No result!';
}
else {
$row = $result->fetch(PDO::FETCH_ASSOC);
return 'Result:' .print_r($row,true);
if(!$result->fetch(PDO::FETCH_ASSOC)) {
return 'No result!';
} else {
$row = $result->fetch(PDO::FETCH_ASSOC);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is the second time $result->fetch() is called, if there are multiple results (there wont be in this example) this will return the second rather than the first one.

return 'Result:' .print_r($row,true);
}
```

Expand Down
11 changes: 5 additions & 6 deletions ru/extending-modx/xpdo/class-reference/xpdo/xpdo.query.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ xPDOObject|false query (string $statement)

```php
$result = $modx->query("SELECT * FROM modx_users WHERE id=1");
if (!is_object($result)) {
return 'No result!';
}
else {
$row = $result->fetch(PDO::FETCH_ASSOC);
return 'Result:' .print_r($row,true);
if(!$result->fetch(PDO::FETCH_ASSOC)) {
return 'No result!';
} else {
$row = $result->fetch(PDO::FETCH_ASSOC);
return 'Result:' .print_r($row,true);
}
```

Expand Down