Jump to content

Recommended Posts

This code is suppose to echo out ALL the custom pages that are in the database, but it's only show ONE. Why is this?

 

	$query_get = mysql_query("SELECT COUNT(id),id,position,content,title FROM custom_pages ORDER BY id");

					while($row = mysql_fetch_assoc($query_get))
					{
						echo '<table border="0">
						<tr><th><p>Title</p><td>'. $row['title'] .'</td></tr>
						<tr><th><p>Position</p><td>'. $row['position'] .'</td></tr>
						<tr><th><p>Action</p><td><a href="index.php?pages=1&delete='. $row['id'] .'">Delete</a> or <a href="index.php?pages=1&edit='. $row['id'] .'">Edit</a></td></tr>
						</table>
						';
					}

Link to comment
https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/
Share on other sites

The code you posted outputs data for each row the query returns (which I just tested.) If you are only getting one set of data, your query is only matching one row OR your actual code is doing something to prevent all the data from being iterated over.

The code you posted outputs data for each row the query returns (which I just tested.) If you are only getting one set of data, your query is only matching one row OR your actual code is doing something to prevent all the data from being iterated over.

 

If you tested it and it works, why wouldn't mine work.....

My data:

 

--
-- Table structure for table `custom_pages`
--

CREATE TABLE `custom_pages` (
  `id` int(11) NOT NULL auto_increment,
  `position` int(11) NOT NULL,
  `content` text collate latin1_general_ci NOT NULL,
  `title` varchar(30) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4 ;

--
-- Dumping data for table `custom_pages`
--

INSERT INTO `custom_pages` VALUES(1, 0, 'Awesome.', '0');
INSERT INTO `custom_pages` VALUES(2, 1, 'Awesome.', 'Awesome.');
INSERT INTO `custom_pages` VALUES(3, 0, 'A', 'A');

well i don't see you actually making use of that count(id) in your code, is it farther down your code and you just didn't post it?

 

$count = myql_num_rows($query_get);

 

would give you the same thing as

 

$row['count(id)']

 

so you can just remove the count(id) from your select statement.

 

Alright, the problem has been fixed. But now, all the results show up besides the very first one. Why is this?

 

$query_get = mysql_query("SELECT id,position,content,title FROM custom_pages");
					$result = mysql_fetch_assoc($query_get);
					echo 	
						'<div class="post">
						<div class="postheader"><h1>Custom Pages</h1></div>
						<div class="postcontent">
						<p>Welcome to the custom pages section. You can delete, edit, and add custom pages here. To add a custom page, click <a href="index.php?pages=1&add_page=1">here</a>.</p>
					';

					while($row = mysql_fetch_assoc($query_get))
					{
						echo '<table border="0">
						<tr><th><p>Title</p></th><td>'. $row['title'] .'</td></tr>
						<tr><th><p>Position</p></th><td>'. $row['position'] .'</td></tr>
						<tr><th><p>Action</p></th><td><a href="index.php?pages=1&delete='. $row['id'] .'">Delete</a> or <a href="index.php?pages=1&edit='. $row['id'] .'">Edit</a></td></tr>
						</table>
						';
					}

					echo 
					'
					<p></p>
					</div>
					<div class="postfooter"></div>
					</div>
					';

umm...I don't get it. First you said the problem was that only the first one was showing.  Now you are saying there's a new problem, that all of them are showing?  Isn't that what you wanted?

 

From your OP:

This code is suppose to echo out ALL the custom pages that are in the database, but it's only show ONE. Why is this?

 

edit:

 

Oh wait, you are saying all of them besides the first one:

 

It's because you have that mysql_fetch_assoc before the while loop.  Remove that.  Each time you call it, it moves the internal pointer of the  result source var up one, so when you start your while loop, it's already been bumped once.

umm...I don't get it. First you said the problem was that only the first one was showing.  Now you are saying there's a new problem, that all of them are showing?  Isn't that what you wanted?

 

From your OP:

This code is suppose to echo out ALL the custom pages that are in the database, but it's only show ONE. Why is this?

 

edit:

 

Oh wait, you are saying all of them besides the first one:

 

It's because you have that mysql_fetch_assoc before the while loop.  Remove that.  Each time you call it, it moves the internal pointer of the  result source var up one, so when you start your while loop, it's already been bumped once.

 

$row = mysql_fetch_assoc($query_get);
					while($row)
					{
						echo '<table border="0">
						<tr><th><p>Title</p></th><td>'. $row['title'] .'</td></tr>
						<tr><th><p>Position</p></th><td>'. $row['position'] .'</td></tr>
						<tr><th><p>Action</p></th><td><a href="index.php?pages=1&delete='. $row['id'] .'">Delete</a> or <a href="index.php?pages=1&edit='. $row['id'] .'">Edit</a></td></tr>
						</table>
						';
					}

 

Not to sure on what you mean. But I tried doing it like you said, and I just got a run-on loop. How am I suppose to do it AFTER the while loop?

you were supposed to remove the one before the while loop.

 

$query_get = mysql_query(...);
while($row = mysql_fetch_assoc($query_get)) {
// html stuff here
}

 

My suggestion for you is to read the manual entry for mysql_fetch_assoc so that you understand how it works.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.