SN1P3R_85 Posted September 2, 2008 Share Posted September 2, 2008 Ok, so i have a website i've been working on for a couple weeks now, and i decided to add a forum. Im not sure if my forum code is conventional, i just made it all up, so im guessing there is a better way to do it. I still want to try to get this one to work however. My code doesn't have any errors, but it doesn't do what it should. here is my code: <?php session_start(); include( '../SQL_PASS.inc' ); //includes my sql info include( '../user.inc' ); //includes user validation, with some user info $_SESSION['return_url'] = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; //just remembers this page if (!$con) { die('Cannot connect to MySQL server: ' . mysql_error()); } if (!$db_select) //$db_select just selects my database { die('Cannon select database: ' . mysql_error()); } $sql_fetch = "SELECT * FROM test_forum"; //selects everything inside the table `test_forum` $sql_query = mysql_query($sql_fetch); //runs the query to select it all if (!$sql_query) { die('Could not run query: ' . mysql_error()); } if (mysql_num_rows($sql_query)==0) //if the table is empty, kills the program { die('there are no entries for this topic'); } if ($row = mysql_fetch_assoc($sql_query)) //this is where the problem is i think. { $max_id = max($row['Id']); //the highest value in the array $row $min_id = min($row['Id']); //the lowest value in $row for ($i=$max_id; $i>=$min_id; $i--) //I want this to print the message in the 'User_msg' column for the highest Id, and then //-go to the Id below it and print the message in that row, and so on. { echo "<p class=\"reg_warn\">" . $row['User_msg'] . ": " . "</p>"; } } ?> What i basically want to do is make an array of the Id column, and then print the information out of that column, and then the next loop, change to a row right below it, (ex. print message in column 17, then 16, then 15, etc). all suggestions are appreciated, except please don't just rewrite my code, and give it back without an explination for why and how it works, thanks Link to comment https://forums.phpfreaks.com/topic/122321-for-loop-and-mysql-help/ Share on other sites More sharing options...
tibberous Posted September 2, 2008 Share Posted September 2, 2008 Yeah, your doing it wrong. mysql_fetch_assoc returns an array as a row. So, if you want to get all the rows, you have to call it a bunch of times. So make this: if ($row = mysql_fetch_assoc($sql_query)) this: while($row = mysql_fetch_assoc($sql_query)){ // Some crap... print_r($row); } And see what it does. Link to comment https://forums.phpfreaks.com/topic/122321-for-loop-and-mysql-help/#findComment-631635 Share on other sites More sharing options...
SN1P3R_85 Posted September 2, 2008 Author Share Posted September 2, 2008 well, its better than what i had. The thing is, i don't just wanna go down my sql table and print each one out, i want print elements from each row out, going from the row containing the highest Id, and down to the one with the lowest, printing every row in that order. Link to comment https://forums.phpfreaks.com/topic/122321-for-loop-and-mysql-help/#findComment-631643 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.