I am trying to switch my code to PDO from basic mysql/php which I can do for inserts, but I'm having some trouble with select so far. The below code is supposed to display the category for a user post at category, but i get no results and when I do var_dump() I get the results down at the bottom.
$user_ID = get_current_user_id();
$the_SID = ( isset( $_GET['writing'] ) ) ? $_GET['writing'] : false;
$results = $dbh->prepare("select
wp_users.ID,
wp_users.display_name,
stories.ID,
stories.SID,
stories.name,
stories.category,
writing.ID,
writing.text
FROM writing
LEFT JOIN stories on writing.SID = stories.SID
LEFT JOIN wp_users ON writing.ID = wp_users.ID
WHERE (stories.SID = $the_SID) AND (writing.approved = 'Y')
order by position desc limit 1");
$results->bindParam(':wp_users.ID', $user_ID, PDO::PARAM_INT);
$results->bindParam(':display_name', $display_name, PDO::PARAM_STR);
$results->bindParam(':stories.ID', $ID, PDO::PARAM_INT);
$results->bindParam(':name', $story_name, PDO::PARAM_STR);
$results->bindParam(':category', $category, PDO::PARAM_STR);
$results->bindParam(':writing.ID', $ID, PDO::PARAM_STR);
$results->bindParam(':text', $text, PDO::PARAM_STR);
$results->execute();
Category: <?php echo $results[0]->category ?><br />
//also tried without [0] but neither works:
Category: <?php echo $results->category ?><br />
echo var_dump($results);
When I load the page I get no results shown for category and the var_dump just displays this, which is basically just printing the first half of the sql command as if I did't have it in quotes or something:
//RESULTS RETURNED AND PRINTED ON WEBPAGE
Category:
object(PDOStatement)#77 (1) {
["queryString"]=>
string(457) “select
wp_users.ID,
wp_users.display_name,
stories.ID,
stories.SID,
stories.name,
stories.category,
writing.ID,
writing.text
FROM writing
LEFT JOIN stories on writing.SID = stories.SID
LEFT JOIN wp_users ON writing.ID = wp_users.ID
WHERE (stories.SID = 18)
order by position desc limit 1″
}