I'm connecting to a MySQL database via PDO, but when I try to execute the SQL call in PHP it fails with the error below. When I echo out the SQL and run it in MySQL Workbench, it returns exactly what I'd expect it to. This is the output of $prepared->errorInfo()
when it fails:
Array (
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near "5" at line 17
)
So it looks like something is wrong with the prepared statement and passing values to it upon execute
, but I'm not sure what. http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html#error_er_parse_error doesn't really help other than telling me the same issue.
function __construct($params = array()) {
$DB = new PDO('mysql:host='.$config['host'].';port='.$config['port'].';dbname='.$config['name'], $config['user'], $config['pswd']);
if (!$DB) {
throw new Exception('Could not connect to database');
}
$type = $params['topic'];
if (isset($params['topic']) && $params['topic'] !== '') {
$typeSlugSql = "SELECT id from topics WHERE slug = ? LIMIT 1";
$prepared = $DB->prepare($topicSlugSql);
$list = $prepared->execute(array($params['topic'])); //NOTE: this is working correctly and returns the ID I expect
if (!$list) {
throw new Exception('topic slug execute failed');
}
$row = $prepared->fetch(PDO::FETCH_ASSOC);
$topic = $row['id']; //id is correctly 6 at this point
}
//generate SQL query
$sql = "SELECT
articles.id AS id,
articles.slug AS slug,
articles.title AS title,
articles.headline AS headline,
articles.description AS description,
articles.keywords AS keywords,
articles.content AS content,
DATE(articles.published_date) AS date,
articles.created AS created
FROM
articles articles
WHERE
articles.published_date <= CURRENT_TIMESTAMP
AND (articles.published IS NOT NULL OR articles.published = 1)
ORDER BY
articles.published_date DESC LIMIT 0 , ?";
//run the query and get the feed data
$prepared = $DB->prepare($sql);
$items = $prepared->execute(array(5)); //this will make the SQL fail...
echo $sql;
if (!$items) {
print_r($DB->errorInfo()); //this prints the 'errorInfo' listed above
throw new Exception('execution error...');
}
$options = array('ellipsis' => '...', 'exact' => false, 'html' => false);
while ($row = $prepared->fetch(PDO::FETCH_ASSOC)) {
array_push($this->resources, new Resource($row['title'], $row['slug'], $row['date'], $this->cleanAndTruncate($row['content'])));
}
}
The weirdest part is that $prepared->execute(array($params['type']))
works correctly and returns the ID I'm expecting. Again, if I copy/paste the echo
'd SQL and replace ?
with 5
it works as expected in MySQL Workbench. If I do:
$sql = "SELECT
articles.id AS id,
articles.slug AS slug,
articles.title AS title,
articles.headline AS headline,
articles.description AS description,
articles.keywords AS keywords,
articles.content AS content,
DATE(articles.published_date) AS date,
articles.created AS created
FROM
articles articles
WHERE
articles.published_date <= CURRENT_TIMESTAMP
AND (articles.published IS NOT NULL OR articles.published = 1)
ORDER BY
articles.published_date DESC LIMIT 0 , 5";
$prepared = $DB->prepare($sql);
$items = $prepared->execute();
It works in PDO and MySQL Workbench, but I need the LIMIT
to change depending on parameters passed in. Any suggestions to figuring out what's breaking this?