从PHP / SQLite中的Query返回字符串

I am attempting to create a cookie with the return string of this query:

$id = $database -> query("SELECT id FROM accounts WHERE username = '$username' AND password = '$password'");

However, it is returning an object rather than a string. I am trying to return the "id" of the row. How do I do this?

You need to extract the array object so that you can access the id string!

$id = $this->db->get()->row_array()->('id');

then you can use $id and set it in a cookie:

setcookie("session", "$id", time()+3600);

You get an object because you executed the query only.

For this you have to fetch data from it, Like

$result = $database -> query("SELECT id FROM accounts WHERE username = '$username' AND password = '$password'");

then if you are using sqlite then,

$data = sqlite_fetch_array($result, SQLITE_ASSOC);
$id=$data['id'];

Refer http://www.php.net/manual/en/function.sqlite-fetch-array.php

then setcookie like:

setcookie("session", "$id", time()+3600);

Refer http://php.net/manual/en/function.setcookie.php

If you are using PDO then

try it like:

$result=$database->query("SELECT id FROM accounts WHERE username = '$username' AND password = '$password'");
if ($result) {
  $row = $result->fetch(PDO::FETCH_ASSOC);
  echo "<pre>";
  print_r($row);
  echo "</pre>";
}

Test this How to get first row of data in sqlite3 using php PDO

You can set what format PDO returns when fetching,

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

Or you can just return the column itself,

http://php.net/manual/en/pdostatement.fetchcolumn.php