Jump to content

Creating a loop


textbox

Recommended Posts

Hello, i wish to create a loop to pull all the information out of my table, instead of just one line.

 

<?php
	//query the database for the comments for this user
	$sql = "SELECT * FROM `comments` WHERE `to` = '$username'";

$result = mysql_query($sql) OR die(mysql_error());
$num=mysql_num_rows($result); 
$from=mysql_result($result,0,"from");
$to=mysql_result($result,0,"to");
$comment=mysql_result($result,0,"comment");

$spacer = "    -    ";
$printcomments = "$from$spacer$comment";

 

Any help would be great!!

Link to comment
https://forums.phpfreaks.com/topic/52357-creating-a-loop/
Share on other sites

This is the code to output multiple database entries with a loop.

 

<?php

$query = mysql_query("SELECT * FROM table WHERE id = 1");

while($row = mysql_fetch_assoc($query)) {
  echo "ID: $row['id']<br />Name: $row['name']<br /><br />";
}

?>

 

Can be adapted to suit your needs :)

Link to comment
https://forums.phpfreaks.com/topic/52357-creating-a-loop/#findComment-258377
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.