-3

I'm trying to get an array from a database, and set the result as a variable (in this case, named '$Rate'). The code which worked using mysqli is this:

//Getting Hire Rate
$Rate = array();
$ratequery = "SELECT HireRate FROM movie WHERE MovieID LIKE '$MovieID'";
$rate = mysqli_query($con, $ratequery)
    or die("Error: ".mysqli_error($con));
while ($row = mysqli_fetch_array($rate, MYSQLI_ASSOC)) {
$Rate[] = $row['HireRate'];
};
mysqli_free_result($rate);

I'd like to do the same using PDO. I've read various pages but cannot get it to work.

Mr Ed
  • 33
  • 1
  • 6
  • You can use [`fetchAll(PDO::FETCH_COLUMN, 0)`](http://php.net/manual/en/pdostatement.fetchall.php) for this and there are lots of examples in the manual – kero Nov 23 '14 at 13:26

2 Answers2

0

Use PDO execute method instead of query because execute prevents you SQL attack

$params = array("%$MovieID%");
$stmt = $handle->prepare("SELECT HireRate FROM movie WHERE MovieID LIKE ?");
$stmt->execute($params);
$row = $sql->fetchAll();
foreach($data as $row) {
    $Rate[] = $row['HireRate'];
}
Asik
  • 7,967
  • 4
  • 28
  • 34
-1

This won't work?

$sql = "SELECT HireRate FROM movie WHERE MovieID LIKE '$MovieID'";
foreach ($conn->query($sql) as $row) {
    print $row['HireRate'];
}

http://php.net/manual/en/pdo.query.php