0

I am trying to put a list of dates that I have stored in my database in an array. I tried the following function but I only get "array" back. I'm not sure what I'm doing wrong or if there's another way to retrieve the dates.

function getdate($id) {

   $select = $this->db->query("select * from dates where user_id= '$id' ");
   $result_array = array();

   while($row = $select->fetch( PDO::FETCH_ASSOC )){
        $result_array[] = $row['date'];
   }

   return $result_array;

}
3
  • which means you are getting an array isn't that what you wanted? Commented Sep 29, 2016 at 0:20
  • @S.Al: First of all you try the same query with a desired $id in the phpmyadmin or terminal, and see whether it's results some rows or not... Commented Sep 29, 2016 at 0:26
  • echo $array; if $array is an array will return 'Array' print_r($array); if you want to see its contents Commented Sep 29, 2016 at 0:26

2 Answers 2

1

While using the PDO statements you could follow up the procedure where PDO is strong enough to avoid SQL Injections. But you have not followed up the process in your code. Usage of bind parameters is advised.

Note: Your code will work well except the fetch statement. You have to fetchAll(); so that it is able to capture all the data from the resultant query and you can print it.

If this is the case you have to use fetchAll(); for retrieving multiple lines of output from the DB or else for retrieving the singe line of output your code will look fine.

Solution One:

You can use bind parameters in the function so that it avoids SQL Injections.

functions.php

function getdate($id)
{
$select = $conn->prepare("SELECT `date` FROM `dates` WHERE user_id= :id ");
$select->bindValue(':id', $id, PDO::PARAM_INT);
$select->execute();
$result = $select->fetchAll(PDO::FETCH_ASSOC);
return $result;
}

Other File you can get the function call like this.

$result = getdate(3);

And you have to print_r() the $result. So that it will be resulting in the array() structure.

Some more clear explanations regarding the fetch statements in PDO.

PDO has some extremely handy methods to return the query result in different formats:

  1. fetch() - a general purpose fetch method similar to mysql_fetch_array().
  2. fetchAll() - to get all the rows without while loop.
  3. fetchColumn() - to get single scalar value without getting array first.

Example:

$stmt = $pdo->prepare("SELECT id,name FROM songs WHERE dt=curdate()");
$stmt->execute();
$data = $stmt->fetchAll();

Now we have all the songs in the $data array and we can desing the page as we want.

<table>
<?php foreach ($data as $row): ?>
  <tr>
    <td>
      <a href="songs.php?<?=$row['id']?>">
        <?=htmlspecialchars($row['name'])?>
      </a>
    </td>
  </tr>
<?php endforeach ?>
</table>
Sign up to request clarification or add additional context in comments.

Comments

1

If you're expecting more than one row it should be "fetchAll". Also, if you don't need everything just get the columns you need.

This is modified from the PHP PDO documentation for fetchAll, I've assumed your user ID is an integer...

$sth = $dbh->prepare("SELECT date FROM dates WHERE user_id= :userid ");
$sth->bindValue(':userid', $id, PDO::PARAM_INT);
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
print_r($result);

The print_r will show you what the array looks like so you can then access it, as nogad says.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.