LemonInflux Posted September 29, 2007 Share Posted September 29, 2007 Hello. First of all, excuse the simplicity of the question, I'm only just starting :-) Ok, a while back, I built a totally flat forum. It worked, (and works) fine, it's just I decided I can't stay using flatfiles forever. So, I decided I'd make a MySQL forum. So, I've built every page I reckon I'll need, it's just now I'm coding it. So, here's my question unto you: Where's the error? <?php $sql = 'SELECT * FROM `forums` LIMIT 0, 30 '; while ($row = mysql_fetch_array($sql) { echo ('Forum 1: '. $row['forum_name'] .'<br><br>'. $row['forum_desc'] .'<br><br>'); } ?> I'm trying to list each forum like this: Forum Name Forum Description Basic for now, I'll build it up later. I'm sure it's a very simple error. Thankyou kindly, Tom. Quote Link to comment Share on other sites More sharing options...
sasa Posted September 29, 2007 Share Posted September 29, 2007 <?php $sql = 'SELECT * FROM `forums` LIMIT 0, 30 '; $result = mysql_query($sql) or die(mysql_error()); // <-- inser this line while ($row = mysql_fetch_array($result) { echo ('Forum 1: '. $row['forum_name'] .'<br><br>'. $row['forum_desc'] .'<br><br>'); } ?> Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted September 29, 2007 Author Share Posted September 29, 2007 didn't work. If anyone's interested, here's the code I connect to the db with: <?php $dbHost = "*"; //Location Of Database usually its localhost $dbUser = "*"; //Database User Name $dbPass = "*"; //Database Password $dbDatabase = "*"; //Database Name $db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database."); mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database."); ?> Quote Link to comment Share on other sites More sharing options...
hemlata Posted September 29, 2007 Share Posted September 29, 2007 Hello, There are 2 Basic mistakes in your code which let you error. - Missing with the closing brace for while condition. See your code.. while ($row = mysql_fetch_array($sql) { and it should be.. while ($row = mysql_fetch_array($sql)) { - Another big mistake is you were fetching records without executing the query. See your code below.. $sql = 'SELECT * FROM `forums` LIMIT 0, 30 '; while ($row = mysql_fetch_array($sql) { and it should be.. $sql = mysel_query('SELECT * FROM `forums` LIMIT 0, 30 ') or die(mysql_error()); while ($row = mysql_fetch_array($sql)) { Hope this will solve your issue. Regards Quote Link to comment Share on other sites More sharing options...
markjoe Posted September 29, 2007 Share Posted September 29, 2007 Well, first of all, what happens? Any error output? <?php $sql = 'SELECT * FROM `forums` LIMIT 0, 30 '; while ($row = mysql_fetch_assoc($sql)) { echo 'Forum 1: '. $row['forum_name'] .'<br><br>'. $row['forum_desc'] .'<br><br>'; } ?> mysql_fetch_assoc() VS. mysql_fetch_array() should not make a difference, I am just used to the former. -The closing ) of the while is missing. -Here is a quote form php.net regarding echo: echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses. hmm, 3 new replies already, oh well, have mine too. oh nice, I missed the lack of query execution... Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted September 29, 2007 Author Share Posted September 29, 2007 hemlata, the first 2 things are identical, aren't they o_O Markjoe, the first time there was just a blank page, now after using your code, the area where the forums should be listed is blank. EDIT: Saw the difference on hemlata's. Quote Link to comment Share on other sites More sharing options...
markjoe Posted September 29, 2007 Share Posted September 29, 2007 while ($row = mysql_fetch_array($sql) { and while ($row = mysql_fetch_array($sql)) { are very different! My posted code was incorrect, I missed the fact that the query execution was missing. <?php $sql = 'SELECT * FROM `forums` LIMIT 0, 30 '; $sqlresult = mysql_query($sql); while ($row = mysql_fetch_assoc($sqlresult)) { echo 'Forum 1: '. $row['forum_name'] .'<br><br>'. $row['forum_desc'] .'<br><br>'; } ?> Quote Link to comment Share on other sites More sharing options...
LemonInflux Posted September 29, 2007 Author Share Posted September 29, 2007 Wahay! That worked perfectly, thanks! Quote Link to comment 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.