5 Replies - 671 Views - Last Post: 07 June 2015 - 02:13 PM Rate Topic: -----

#1 ricososwavay   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 113
  • Joined: 11-February 14

Fetch row from prepared statement

Posted 07 June 2015 - 11:11 AM

I am trying to use a prepared statement to fetch the rows and put into an array. I am then displaying the comments in a foreach statement.but I am having a problem with the fetching of the columns. I have a problem with the while statement. This works if I dont use a prepared statement but I need to prevent sql injections.

$query = $db->prepare("
	SELECT 
        comments.userId,
	comments.id, 
        comments.username,
        comments.joinDate,
        comments.date,
	comments.comment,
	COUNT(comment_likes.id) AS likes,
        COUNT(comment_dislikes.id) AS dislikes
	

	FROM comments

	LEFT JOIN comment_likes
	ON comments.id = comment_likes.comment_id
        
        LEFT JOIN comment_dislikes
	ON comments.id = comment_dislikes.comment_id

	LEFT JOIN users
	ON comment_likes.user_id = users.id AND comment_dislikes.user_id = users.id
        

	GROUP BY comments.id
	");
$query->execute();
if (!$query) {
    die($db->error);
} else {

    while ($row = $query->fetch(PDO::FETCH_OBJ)) {
        
        $comments[] = $row;
        
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Fetch row from prepared statement

#2 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Fetch row from prepared statement

Posted 07 June 2015 - 11:22 AM

What problem are you having? Are there errors? Just saying "I have a problem" isn't telling us anything useful.

This post has been edited by andrewsw: 07 June 2015 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#3 ricososwavay   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 113
  • Joined: 11-February 14

Re: Fetch row from prepared statement

Posted 07 June 2015 - 11:32 AM

The error I am getting is that mysqli::fetch() expects 0 parameters 1 given, but even when I specify no parameters I get a new error stating trying to get property of non-object. So i try fetch_object() and get this error Call to undefined method mysqli_stmt::fetch_object().
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: Fetch row from prepared statement

Posted 07 June 2015 - 11:52 AM

You are mixing use of the mysqli and PDO libraries. Use one or the other. (The error message refers to mysqli, but you attempt to use a PDO constant.)

Polite request: please always post the error details with your question, people shouldn't have to request this information.

This post has been edited by andrewsw: 07 June 2015 - 11:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 ricososwavay   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 113
  • Joined: 11-February 14

Re: Fetch row from prepared statement

Posted 07 June 2015 - 12:18 PM

This is the original query I am trying to change into a prepared statement. So how do I change this to a prepared statement. The code above is what I have tried.

$query = $db->query("
	SELECT 
        comments.userId,
	comments.id, 
        comments.username,
        comments.joinDate,
        comments.date,
	comments.comment,
	COUNT(comment_likes.id) AS likes,
        COUNT(comment_dislikes.id) AS dislikes
	

	FROM comments

	LEFT JOIN comment_likes
	ON comments.id = comment_likes.comment_id
        
        LEFT JOIN comment_dislikes
	ON comments.id = comment_dislikes.comment_id

	LEFT JOIN users
	ON comment_likes.user_id = users.id AND comment_dislikes.user_id = users.id
        

	GROUP BY comments.id
	");

if (!$query) {
    die($db->error);
} else {

    while ($row = $query->fetch_object()) {
        
        $comments[] = $row;
        
    }
}

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: Fetch row from prepared statement

Posted 07 June 2015 - 02:13 PM

Quote

This is the original query I am trying to change into a prepared statement.

there are no user provided data, hence no need to use a Prepared Statement.

second, line #28: $query is an object, it will never evaluate as false.

third, from the looks of it thatís mysqli not PDO.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1