3raser Posted August 14, 2010 Share Posted August 14, 2010 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> '; } Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/ Share on other sites More sharing options...
.josh Posted August 14, 2010 Share Posted August 14, 2010 either your table only has 1 row to return (calling the wrong table? check your db directly) or the lack of closing </th> tags might possibly be doing something funky with how the html is rendered. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099086 Share on other sites More sharing options...
3raser Posted August 15, 2010 Author Share Posted August 15, 2010 I fixed that, but it stills does it. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099392 Share on other sites More sharing options...
PFMaBiSmAd Posted August 15, 2010 Share Posted August 15, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099394 Share on other sites More sharing options...
3raser Posted August 15, 2010 Author Share Posted August 15, 2010 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..... Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099405 Share on other sites More sharing options...
PFMaBiSmAd Posted August 15, 2010 Share Posted August 15, 2010 We only see the information you supply in your posts. It is kind of up to you to investigate what your code is doing on your server with your data. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099406 Share on other sites More sharing options...
MadTechie Posted August 15, 2010 Share Posted August 15, 2010 The "code" is fine, however we can't test your data, if you give us some sample data that should work but doesn't then we can help, however everything points to that being the problem! Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099410 Share on other sites More sharing options...
ngreenwood6 Posted August 15, 2010 Share Posted August 15, 2010 If I am not mistaken a query with a count(*) clause in it only returns one result. If you want it to return more than one row take that out and use mysql_num_rows or something else to count the results. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099430 Share on other sites More sharing options...
.josh Posted August 15, 2010 Share Posted August 15, 2010 ooh good call, I didn't even bother to look at the query since he said it was returning results Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099436 Share on other sites More sharing options...
PFMaBiSmAd Posted August 15, 2010 Share Posted August 15, 2010 Mmmm and the data I used to test the functioning of the code was just that, some expected data. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099440 Share on other sites More sharing options...
3raser Posted August 16, 2010 Author Share Posted August 16, 2010 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'); Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099699 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2010 Share Posted August 16, 2010 So, did you read what ngreenwood6 wrote in his post? Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099700 Share on other sites More sharing options...
3raser Posted August 16, 2010 Author Share Posted August 16, 2010 Yes, but how will that work?.....I'm trying to grab data and see if it exists at the same time....not possible with mysql_num_rows is it? Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099702 Share on other sites More sharing options...
MadTechie Posted August 16, 2010 Share Posted August 16, 2010 Here's an idea use mysql_fetch and mysql_num_rows.. also if you grab the data then it must exist! Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099703 Share on other sites More sharing options...
.josh Posted August 16, 2010 Share Posted August 16, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1099849 Share on other sites More sharing options...
3raser Posted August 17, 2010 Author Share Posted August 17, 2010 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> '; Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1100073 Share on other sites More sharing options...
Pikachu2000 Posted August 17, 2010 Share Posted August 17, 2010 Because you're echoing them in a while() loop. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1100074 Share on other sites More sharing options...
.josh Posted August 17, 2010 Share Posted August 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1100100 Share on other sites More sharing options...
Pikachu2000 Posted August 17, 2010 Share Posted August 17, 2010 I missed the "besides" as well, so disregard my post above . . . Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1100107 Share on other sites More sharing options...
3raser Posted August 18, 2010 Author Share Posted August 18, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1100578 Share on other sites More sharing options...
.josh Posted August 18, 2010 Share Posted August 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/210687-only-echoing-out-one-result/#findComment-1100585 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.