Skip to main content
added 202 characters in body
Source Link
Clive
  • 167.9k
  • 19
  • 303
  • 337

The return from db_query() is a resource that you can iterate over, it's never a single value.

The way to get a single field from a query is with the fetchField() method:

$result = db_query('SELECT name FROM {users} WHERE uid = 1')->fetchField();

If you've got more than one result then you can iterate over it like so:

foreach ($result as $row) {
  // Do something with $row...
}

or, you can make use of one of the helper methods to get data directly from the query into a local variable. These include:

  • fetchObject()

    Fetches the next row and returns it as an object.

  • fetchAllAssoc()

    Returns the result set as an associative array keyed by the given field.

  • fetchAllKeyed()

    Returns the entire result set as a single associative array.

  • fetchAssoc()

    Fetches the next row and returns it as an associative array.

  • fetchCol()

    Returns an entire single column of a result set as an indexed array.

  • fetchField()

    Returns a single field from the next record of a result set.

The return from db_query() is a resource that you can iterate over, it's never a single value.

The way to get a single field from a query is with the fetchField() method:

$result = db_query('SELECT name FROM {users} WHERE uid = 1')->fetchField();

If you've got more than one result then you can iterate over it like so:

foreach ($result as $row) {
  // Do something with $row...
}

or, you can make use of one of the helper methods to get data directly from the query into a local variable. These include:

  • fetchAllAssoc()

    Returns the result set as an associative array keyed by the given field.

  • fetchAllKeyed()

    Returns the entire result set as a single associative array.

  • fetchAssoc()

    Fetches the next row and returns it as an associative array.

  • fetchCol()

    Returns an entire single column of a result set as an indexed array.

  • fetchField()

    Returns a single field from the next record of a result set.

The return from db_query() is a resource that you can iterate over, it's never a single value.

The way to get a single field from a query is with the fetchField() method:

$result = db_query('SELECT name FROM {users} WHERE uid = 1')->fetchField();

If you've got more than one result then you can iterate over it like so:

foreach ($result as $row) {
  // Do something with $row...
}

or, you can make use of one of the helper methods to get data directly from the query into a local variable. These include:

  • fetchObject()

    Fetches the next row and returns it as an object.

  • fetchAllAssoc()

    Returns the result set as an associative array keyed by the given field.

  • fetchAllKeyed()

    Returns the entire result set as a single associative array.

  • fetchAssoc()

    Fetches the next row and returns it as an associative array.

  • fetchCol()

    Returns an entire single column of a result set as an indexed array.

  • fetchField()

    Returns a single field from the next record of a result set.

added 368 characters in body
Source Link
Clive
  • 167.9k
  • 19
  • 303
  • 337

The return from db_query() is a resource that you can iterate over, it's never a single value.

The way to get a single field from a query is with the fetchField() method:

$result = db_query('SELECT name FROM {users} WHERE uid = 1')->fetchField();

If you've got more than one result then you can iterate over it like so:

foreach ($result as $row) {
  // Do something with $row...
}

or, you can make use of one of the helper methods to get data directly from the query into a local variable. These include:

The return from db_query() is a resource that you can iterate over, it's never a single value.

The way to get a single field from a query is with the fetchField() method:

$result = db_query('SELECT name FROM {users} WHERE uid = 1')->fetchField();

If you've got more than one result then you can iterate over it like so:

foreach ($result as $row) {
  // Do something with $row...
}

or, you can make use of one of the helper methods to get data directly from the query into a local variable. These include:

The return from db_query() is a resource that you can iterate over, it's never a single value.

The way to get a single field from a query is with the fetchField() method:

$result = db_query('SELECT name FROM {users} WHERE uid = 1')->fetchField();

If you've got more than one result then you can iterate over it like so:

foreach ($result as $row) {
  // Do something with $row...
}

or, you can make use of one of the helper methods to get data directly from the query into a local variable. These include:

  • fetchAllAssoc()

    Returns the result set as an associative array keyed by the given field.

  • fetchAllKeyed()

    Returns the entire result set as a single associative array.

  • fetchAssoc()

    Fetches the next row and returns it as an associative array.

  • fetchCol()

    Returns an entire single column of a result set as an indexed array.

  • fetchField()

    Returns a single field from the next record of a result set.

Source Link
Clive
  • 167.9k
  • 19
  • 303
  • 337

The return from db_query() is a resource that you can iterate over, it's never a single value.

The way to get a single field from a query is with the fetchField() method:

$result = db_query('SELECT name FROM {users} WHERE uid = 1')->fetchField();

If you've got more than one result then you can iterate over it like so:

foreach ($result as $row) {
  // Do something with $row...
}

or, you can make use of one of the helper methods to get data directly from the query into a local variable. These include: