3 Replies - 576 Views - Last Post: 30 October 2014 - 07:39 AM Rate Topic: -----

#1 Hypermx   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 06-August 09

MySQLi only outputs one row.

Posted 30 October 2014 - 03:43 AM

Hey.

Im currently trying to make a website where a user can post something and people can reply back.

But I am stuck on a problem now.
My replies section only prints the first reply.

My code:
function get_reply($id, $LIMIT = 3, $mysqli) {
	
	
	if(!($stmt = $mysqli->prepare("SELECT id, uid, datetime, img, comment FROM posts WHERE replyto=? ORDER BY id DESC LIMIT 3"))) {
		echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
	}
	if(!$stmt->bind_param("i", $id)) {
		echo "Binding param failed: (" . $stmt->errno . ") " . $stmt->error;
	}
	if (!$stmt->execute()) {																	//Execute SQL command -- THIS MAKES THINGS  WORK!
		echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;					
	}
	$res = $stmt->get_result();
	$row[] = $res->fetch_assoc();
	
	var_dump($row);
	
	foreach($row as $replies) {
			$apply_style = false;
			if($replies['img'] == "") {
				$apply_style = true;
			}
	?><div class="post_reply_main">
				<div class="post_reply_helper">
					<table <?php if($apply_style) { echo "class='nopic'"; } ?>>
						 <tr>
							<?php if(!$apply_style) { ?><th rowspan="2" class="post_main_img"><img src="<?php echo $replies['img']; ?>" alt=""></th><?php } ?>
							<th class="post_main_header">
								<ul>
									<li>Anon <span style="margin-left: 5px;">(UID: <?php echo $replies['uid']; ?>)</span>
									<li><?php echo $replies['datetime']; ?>
									<li><a href="#reply<?php echo $replies['id'] ?>">Reply</a>
								</ul>
							</th>
						 </tr>
						 <tr>
							<td class="post_reply_comment"><?php echo $replies['comment']; ?></td>
					  </tr>
					</table>
				</div>
			</div><?php
	}
	$res->free();
	$stmt->close();
}
?>




I have attached my table 'posts' below.

If anybody can see where my mistake is, please help me :)/>/>
Thank you.

Attached image(s)

  • Attached Image

This post has been edited by Hypermx: 30 October 2014 - 05:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: MySQLi only outputs one row.

#2 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3151
  • View blog
  • Posts: 11,956
  • Joined: 03-December 12

Re: MySQLi only outputs one row.

Posted 30 October 2014 - 03:45 AM

Data is retrieved one row at a time, that's why examples use loops to retrieve.
Was This Post Helpful? 0
  • +
  • -

#3 Hypermx   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 77
  • Joined: 06-August 09

Re: MySQLi only outputs one row.

Posted 30 October 2014 - 03:52 AM

View Postastonecipher, on 30 October 2014 - 04:45 AM, said:

Data is retrieved one row at a time, that's why examples use loops to retrieve.

I thought i did loop it with the foreach?

Do I need to loop the fetch_assoc also?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

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

Re: MySQLi only outputs one row.

Posted 30 October 2014 - 07:39 AM

View PostHypermx, on 30 October 2014 - 12:52 PM, said:

I thought i did loop it with the foreach?

but you put only a single item in your $row array. and then looped over that.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1